import {
  Controller,
  Post,
  Get,
  Body,
  HttpCode,
  HttpStatus,
  UseGuards,
  UseInterceptors,
  UploadedFiles,
} from '@nestjs/common';
import { FilesInterceptor } from '@nestjs/platform-express';
import { KycService, KycStatusResult } from '@/application/services';
import { JwtAuthGuard } from '@/shared/guards/jwt-auth.guard';
import { CapabilityGuard } from '@/shared/guards/capability.guard';
import { CurrentUser } from '@/shared/decorators/current-user.decorator';
import { RequireCapability } from '@/shared/decorators/require-capability.decorator';

class SubmitKycDto {
  realName: string;
  idCardNo: string;
}

@Controller('kyc')
@UseGuards(JwtAuthGuard, CapabilityGuard)
export class KycController {
  constructor(private readonly kycService: KycService) {}

  /**
   * 获取 KYC 状态
   * GET /kyc/status
   */
  @Get('status')
  async getStatus(
    @CurrentUser() user: { accountSequence: string },
  ): Promise<{ success: boolean; data: KycStatusResult }> {
    const result = await this.kycService.getKycStatus(user.accountSequence);
    return { success: true, data: result };
  }

  /**
   * 提交 KYC 资料
   * POST /kyc/submit
   */
  @Post('submit')
  @RequireCapability('KYC')
  @HttpCode(HttpStatus.OK)
  @UseInterceptors(FilesInterceptor('files', 2))
  async submitKyc(
    @CurrentUser() user: { accountSequence: string },
    @Body() dto: SubmitKycDto,
    @UploadedFiles() files: Express.Multer.File[],
  ): Promise<{ success: boolean }> {
    // TODO: 保存上传的文件并获取路径
    const idCardFront = files?.[0]?.path || 'placeholder-front';
    const idCardBack = files?.[1]?.path || 'placeholder-back';

    await this.kycService.submitKyc({
      accountSequence: user.accountSequence,
      realName: dto.realName,
      idCardNo: dto.idCardNo,
      idCardFront,
      idCardBack,
    });

    return { success: true };
  }
}
