import { Body, Controller, Post, Req, UseGuards } from '@nestjs/common';
import { AuthAdminService } from './authAdmin.service';
import { CreateAdminDto } from 'src/admin/dto/create-admin.dto';
import { LoginAdminDto } from 'src/auth/admin/dto/login-admin.dto';
import { ApiBearerAuth, ApiBody, ApiCreatedResponse, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger';
import { AdminSigninResDto } from './dto/admin-signin-res.dto';
import { Roles } from '../role.decorator';
import { ADMIN_ROLE, ADMIN_ROLES, SUPERADMIN_ROLES } from 'src/shared/enum/admin-role.enum';
import { AuthAdminGuard } from './authAdmin.guard';
import { UpdatePwdDto } from '../user/dto/update-pwd.dto';
import { CustomAdminRequest } from './interface/customAdminReq';
import { AdminRequestCodeBody, AdminResetPasswordBody } from './dto/admin-reset-password.dto';
import { GetResetCode } from './dto/get-reset-code.dto';
import { EmailService } from 'src/email/email.service';

@Controller()
@ApiTags('Create admin account and sign in as admin')
export class AuthAdminController {
  constructor(
    private readonly authAdminService: AuthAdminService,
    private readonly mailService: EmailService,
  ) {}

  @ApiBody({ type: CreateAdminDto })
  @ApiCreatedResponse({
    content: {
      'application/json': {
        example: {
          token: 'token',
        },
      },
    },
  })
  @UseGuards(AuthAdminGuard)
  @Roles(SUPERADMIN_ROLES)
  @Post('admin/signup')
  @ApiBearerAuth('jwt')
  @ApiUnauthorizedResponse()
  async signUp(@Body() createAdminDto: CreateAdminDto) {
    try {
      createAdminDto.role = ADMIN_ROLE.ADMIN;
      const accessToken = await this.authAdminService.signUp(createAdminDto);
      return { token: accessToken };
    } catch (e) {
      throw e;
    }
  }
  @ApiBody({ type: LoginAdminDto })
  @ApiCreatedResponse({
    type: AdminSigninResDto,
  })
  @ApiUnauthorizedResponse()
  @Post('admin/signin')
  async signIn(@Body() loginAdminDto: LoginAdminDto) {
    try {
      const adminSigninResDto = await this.authAdminService.signIn(loginAdminDto);
      return adminSigninResDto;
    } catch (e) {
      throw e;
    }
  }

  @UseGuards(AuthAdminGuard)
  @Roles(ADMIN_ROLES)
  @Post('admin/change-password')
  @ApiBearerAuth('jwt')
  async changePassword(@Req() req: CustomAdminRequest, @Body() updatePwdDto: UpdatePwdDto) {
    const email = req.admin.email;
    return this.authAdminService.changePassword(email, updatePwdDto);
  }

  @ApiBody({ type: AdminResetPasswordBody })
  @Post('admin/reset-password')
  async resetPassword(@Body() resetPasswordDto: AdminResetPasswordBody) {
    return this.authAdminService.resetPassword(resetPasswordDto);
  }

  @ApiBody({ type: AdminRequestCodeBody })
  @Post('admin/get-reset-code')
  async getResetCode(@Body() resetPasswordInfo: GetResetCode) {
    try {
      const code = await this.authAdminService.getResetPasswordCode(resetPasswordInfo.email);
      await this.mailService.sendResetPasswordEmail(resetPasswordInfo.email, code);
    } catch (e) {
      throw e;
    }
  }
}
