import {
  Controller,
  Post,
  Body,
  Req,
  UseGuards,
  Res,
  Get,
} from '@nestjs/common';
import { AuthService } from '../auth.service';
import { LoginDto } from '../dto/login.dto';
import { Request, Response } from 'express';
import { ChangePasswordDto } from '../dto/change-password.dto';
import AppResponse from '@/response/app-response';
import { Throttle } from '@nestjs/throttler';
import { GenerateTokenDto } from '../dto/generate-token.dto';

@Controller('admin/auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Post('login')
  async login(
    @Body() loginDto: LoginDto,
    @Res({ passthrough: true }) response: Response,
  ) {
    return this.authService
      .validateAdmin(loginDto.username, loginDto.password)
      .then((admin) => this.authService.login(admin, response));
  }

  @Post('check-status')
  @Throttle({ default: { limit: 1, ttl: 30 * 1000 } })
  async checkStatus() {
    return AppResponse.toResponse(true);
  }

  @Post('logout')
  async logout(@Res({ passthrough: true }) response: Response) {
    return await this.authService.logout(response);
  }

  @Post('change-password')
  async changePassword(
    @Res({ passthrough: true }) response: Response,
    @Req() request: Request,
    @Body() data: ChangePasswordDto,
  ) {
    return await this.authService.changePassword(data, request, response);
  }

  @Get('me')
  async me(@Req() request: Request) {
    return this.authService.me(request);
  }
}
