import {
  Controller,
  Post,
  Body,
  HttpCode,
  HttpStatus,
  UseGuards,
} from '@nestjs/common';
import { ThrottlerGuard } from '@nestjs/throttler';
import { PasswordService } 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 ResetPasswordDto {
  phone: string;
  smsCode: string;
  newPassword: string;
}

class ChangePasswordDto {
  oldPassword: string;
  newPassword: string;
}

@Controller('auth/password')
@UseGuards(ThrottlerGuard)
export class PasswordController {
  constructor(private readonly passwordService: PasswordService) {}

  /**
   * 重置密码（通过短信验证码）
   * POST /password/reset
   */
  @Post('reset')
  @HttpCode(HttpStatus.OK)
  async resetPassword(
    @Body() dto: ResetPasswordDto,
  ): Promise<{ success: boolean }> {
    await this.passwordService.resetPassword(dto);
    return { success: true };
  }

  /**
   * 修改密码（需要登录）
   * POST /password/change
   */
  @Post('change')
  @HttpCode(HttpStatus.OK)
  @UseGuards(JwtAuthGuard, CapabilityGuard)
  @RequireCapability('PROFILE_EDIT')
  async changePassword(
    @CurrentUser() user: { accountSequence: string },
    @Body() dto: ChangePasswordDto,
  ): Promise<{ success: boolean }> {
    await this.passwordService.changePassword({
      accountSequence: user.accountSequence,
      oldPassword: dto.oldPassword,
      newPassword: dto.newPassword,
    });

    return { success: true };
  }
}
