import { Controller, Get, Post, Body, Param, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { SystemAccountService } from '../../application/services/system-account.service';
import { AdminOnly, Public } from '../../shared/guards/jwt-auth.guard';
import { SystemAccountType } from '@prisma/client';

class InitializeSystemAccountsDto {
  headquarters: { name: string; code: string };
  operation: { name: string; code: string };
  fee: { name: string; code: string };
  hotWallet: { name: string; code: string; blockchainAddress?: string };
  coldWallet?: { name: string; code: string; blockchainAddress?: string };
}

class CreateRegionAccountDto {
  name: string;
  code: string;
  regionId: string;
}

@ApiTags('System Accounts')
@Controller('system-accounts')
@ApiBearerAuth()
export class SystemAccountController {
  constructor(private readonly systemAccountService: SystemAccountService) {}

  @Get()
  @AdminOnly()
  @ApiOperation({ summary: '获取所有系统账户' })
  @ApiResponse({ status: 200, description: '系统账户列表' })
  async findAll() {
    return this.systemAccountService.findAll();
  }

  @Get('by-type/:type')
  @AdminOnly()
  @ApiOperation({ summary: '按类型获取系统账户' })
  async findByType(@Param('type') type: SystemAccountType) {
    return this.systemAccountService.findByType(type);
  }

  @Get('by-code/:code')
  @AdminOnly()
  @ApiOperation({ summary: '按代码获取系统账户' })
  async findByCode(@Param('code') code: string) {
    return this.systemAccountService.findByCode(code);
  }

  @Post('initialize')
  @Public()
  @ApiOperation({ summary: '初始化核心系统账户（仅限内网调用）' })
  @ApiResponse({ status: 201, description: '系统账户初始化成功' })
  async initialize(@Body() dto: InitializeSystemAccountsDto) {
    return this.systemAccountService.initializeCoreAccounts(dto);
  }

  @Post('province')
  @AdminOnly()
  @ApiOperation({ summary: '创建省级公司账户' })
  async createProvinceAccount(@Body() dto: CreateRegionAccountDto) {
    return this.systemAccountService.createProvinceAccount(dto.regionId, dto.name, dto.code);
  }

  @Post('city')
  @AdminOnly()
  @ApiOperation({ summary: '创建市级公司账户' })
  async createCityAccount(@Body() dto: CreateRegionAccountDto) {
    return this.systemAccountService.createCityAccount(dto.regionId, dto.name, dto.code);
  }
}
