import {
  Controller,
  Post,
  Get,
  Body,
  HttpCode,
  HttpStatus,
  UseGuards,
} from '@nestjs/common';
import { IsString, IsNotEmpty } from 'class-validator';
import { ThrottlerGuard } from '@nestjs/throttler';
import { TradePasswordService } from '@/application/services/trade-password.service';
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 SetTradePasswordDto {
  @IsString()
  @IsNotEmpty()
  loginPassword: string;

  @IsString()
  @IsNotEmpty()
  tradePassword: string;
}

class ChangeTradePasswordDto {
  @IsString()
  @IsNotEmpty()
  oldTradePassword: string;

  @IsString()
  @IsNotEmpty()
  newTradePassword: string;
}

class VerifyTradePasswordDto {
  @IsString()
  @IsNotEmpty()
  tradePassword: string;
}

@Controller('auth/trade-password')
@UseGuards(ThrottlerGuard)
export class TradePasswordController {
  constructor(private readonly tradePasswordService: TradePasswordService) {}

  /**
   * 获取支付密码状态
   * GET /trade-password/status
   */
  @Get('status')
  @UseGuards(JwtAuthGuard)
  async getStatus(
    @CurrentUser() user: { accountSequence: string },
  ): Promise<{ hasTradePassword: boolean }> {
    return this.tradePasswordService.getStatus(user.accountSequence);
  }

  /**
   * 设置支付密码（需要验证登录密码）
   * POST /trade-password/set
   */
  @Post('set')
  @HttpCode(HttpStatus.OK)
  @UseGuards(JwtAuthGuard, CapabilityGuard)
  @RequireCapability('PROFILE_EDIT')
  async setTradePassword(
    @CurrentUser() user: { accountSequence: string },
    @Body() dto: SetTradePasswordDto,
  ): Promise<{ success: boolean }> {
    await this.tradePasswordService.setTradePassword({
      accountSequence: user.accountSequence,
      loginPassword: dto.loginPassword,
      tradePassword: dto.tradePassword,
    });

    return { success: true };
  }

  /**
   * 修改支付密码
   * POST /trade-password/change
   */
  @Post('change')
  @HttpCode(HttpStatus.OK)
  @UseGuards(JwtAuthGuard, CapabilityGuard)
  @RequireCapability('PROFILE_EDIT')
  async changeTradePassword(
    @CurrentUser() user: { accountSequence: string },
    @Body() dto: ChangeTradePasswordDto,
  ): Promise<{ success: boolean }> {
    await this.tradePasswordService.changeTradePassword({
      accountSequence: user.accountSequence,
      oldTradePassword: dto.oldTradePassword,
      newTradePassword: dto.newTradePassword,
    });

    return { success: true };
  }

  /**
   * 验证支付密码
   * POST /trade-password/verify
   */
  @Post('verify')
  @HttpCode(HttpStatus.OK)
  @UseGuards(JwtAuthGuard, CapabilityGuard)
  @RequireCapability('TRADING')
  async verifyTradePassword(
    @CurrentUser() user: { accountSequence: string },
    @Body() dto: VerifyTradePasswordDto,
  ): Promise<{ valid: boolean }> {
    const valid = await this.tradePasswordService.verifyTradePassword({
      accountSequence: user.accountSequence,
      tradePassword: dto.tradePassword,
    });

    return { valid };
  }
}
