import { Body, Controller, Get, Headers, Post, Query, Req, Res, UseGuards } from "@nestjs/common";
import { AccountService } from "./account.service";
import { SchedulerDTO as DTO } from "@baekyangdan/core-utils";
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";
import { HttpApiUrl } from '@baekyangdan/core-utils';

const AccountApi = HttpApiUrl.Account;
@UseGuards(JwtAccessAuthGuard)
@Controller(AccountApi.base)
export class AccountController {
  constructor(private readonly accountService: AccountService) {}

  @Get(AccountApi.root)
  async test() {
    return "Test"
  }

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

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

  @Public()
  @Post(AccountApi.verifyEmailVerificationCode)
  async verifyEmailVerificationCode(@Body() body: DTO.VerifyEmailVerificationCodeRequest): Promise<DTO.VerifyEmailVerificationCodeResponse> {
    const result = await this.accountService.verifyCode(body);
    return result;
  }

  @Public()
  @Post(AccountApi.sendPasswordResetCode)
  async sendPasswordResetCode(@Body() body: DTO.SendPasswordResetCodeRequest): Promise<DTO.SendPasswordResetCodeResponse> {
    const result = await this.accountService.sendPasswordResetCode(body);
    return result;
  }

  @Public()
  @Post(AccountApi.verifyPasswordResetCode)
  async verifyPasswordResetCode(@Body() body: DTO.VerifyPasswordResetCodeRequest): Promise<DTO.VerifyPasswordResetCodeResponse> {
    const result = await this.accountService.verifyPasswordResetCode(body);
    return result;
  }

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

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

  @Public()
  @Post(AccountApi.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.data.refreshToken!, {
        httpOnly: true,
        path: '/',
        secure: true,
        maxAge: 7 * 24 * 60 * 60 * 1000
      });
    }
    return result;
  }

  @Public()
  @UseGuards(AuthGuard('refresh-token'))
  @Get(AccountApi.refreshAccessToken)
  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.data.refreshToken!, {
        httpOnly: true,
        path: '/',
        secure: true,
        maxAge: 7 * 24 * 60 * 60 * 1000
      });
      
      return result;
    }
    return result;
  }
}