import { Body, Controller, Get, Headers, Post, Query, Req, Res, UseGuards } from "@nestjs/common";
import { AccountService } from "./account.service";
import * as DTO from "./dto";
import { Public } from "src/common/decorators/public.decorator";
import type { FastifyReply, FastifyRequest } from "fastify";
import { AuthGuard } from "@nestjs/passport";
import { JwtAccessAuthGuard } from "src/middleware/auth/guard/access-token.guard";

@UseGuards(JwtAccessAuthGuard)
@Controller('account')
export class AccountController {
  constructor(private readonly accountService: AccountService) {}

  @Get('/')
  async test() {
    return "Test"
  }

  @Public()
  @Get('check-duplication')
  async checkDuplication(@Query() query: DTO.CheckDuplicationRequest): Promise<DTO.CheckDuplicationResponse> {
    return await this.accountService.checkDuplication(query);
  }

  @Public()
  @Post('send-email-verification-code')
  async sendEmailVerificationCode(@Body() body: DTO.SendEmailVerificationCodeRequest): Promise<DTO.SendEmailVerificationCodeResponse> {
    const result = await this.accountService.sendVerificationCode(body);
    return result;
  }

  @Public()
  @Post('verify-email-verification-code')
  async verifyCode(@Body() body: DTO.VerifyEmailVerificationCodeRequest): Promise<DTO.VerifyEmailVerificationCodeResponse> {
    const result = await this.accountService.verifyCode(body);
    return result;
  }

  @Public()
  @Post('send-reset-password-code')
  async sendResetPasswordCode(@Body() body: DTO.SendResetPasswordCodeRequest): Promise<DTO.SendResetPasswordCodeResponse> {
    const result = await this.accountService.sendResetPasswordCode(body);
    return result;
  }

  @Public()
  @Post('verify-reset-password-code')
  async verifyResetPasswordCode(@Body() body: DTO.VerifyResetPasswordCodeRequest): Promise<DTO.VerifyResetPasswordCodeResponse> {
    const result = await this.accountService.verifyResetPasswordCode(body);
    return result;
  }

  @Public()
  @Post('reset-password')
  async resetPassword(@Body() body: DTO.ResetPasswordRequest): Promise<DTO.ResetPasswordResponse> {
    const result = await this.accountService.resetPassword(body);
    return result;
  }

  @Public()
  @Post('signup')
  async signup(@Body() body: DTO.SignupRequest): Promise<DTO.SignupResponse> {
    const result = await this.accountService.signup(body);
    return result;
  }

  @Public()
  @Post('login')
  async login(@Body() body: DTO.LoginRequest, @Res({ passthrough: true }) res: FastifyReply): Promise<DTO.LoginResponse> {
    const result = await this.accountService.login(body);
    if (result.success) {
      res.setCookie('refresh_token', result.refreshToken!, {
        httpOnly: true,
        path: '/',
        secure: true,
        maxAge: 7 * 24 * 60 * 60 * 1000
      });
    }
    return { 
      success: result.success,
      message: result.message,
      error: result.error,
      accessToken: result.accessToken
    };
  }

  @Public()
  @UseGuards(AuthGuard('refresh-token'))
  @Get('refresh-access-token')
  async refreshAccessToken(@Req() req, @Res({ passthrough: true }) res: FastifyReply): Promise<DTO.RefreshAccessTokenResponse> {
    const result = await this.accountService.refreshAccessToken(req.user.id);
    if (result.success) {
      res.setCookie('refresh_token', result.refreshToken!, {
        httpOnly: true,
        path: '/',
        secure: true,
        maxAge: 7 * 24 * 60 * 60 * 1000
      });
      
      return {
        success: result.success,
        message: result.message,
        error: result.error,
        accessToken: result.accessToken
      }
    }
    return result;
  }
}