import {
  Body,
  Controller,
  Get,
  Param,
  Post,
  Req,
  UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { ExcludePrivilege, Public } from 'src/core/guards';
import { MODULE_NAME } from 'src/core/strings/constants/module.constants';
import { OtpVerificationService } from '../data/services/otp-verification.service';
import {
  OtpRequestDto,
  OtpVerifierCreateDto,
  OtpVerifyDto,
} from './dto/otp-verification.dto';
import { OtpAuthGuard } from './guards/otp-auth.guard';
import { OtpVerifierService } from '../data/services/otp-verifier.service';

@ApiTags(`${MODULE_NAME.OTP_VERIFICATIONS.split('-').join(' ')} - data`)
@Controller(`v1/${MODULE_NAME.OTP_VERIFICATIONS}`)
@Public()
export class OtpVerificationController {
  constructor(
    private readonly otpVerificationService: OtpVerificationService,
  ) {}

  @Post('request')
  @UseGuards(OtpAuthGuard)
  async request(@Body() body: OtpRequestDto, @Req() req) {
    return await this.otpVerificationService.requestOTP(body, req);
  }

  @Post('verify')
  @UseGuards(OtpAuthGuard)
  async verify(@Body() body: OtpVerifyDto, @Req() req) {
    return await this.otpVerificationService.verifyOTP(body, req);
  }

  @Get(':ref_or_target_id')
  async getByPhoneNumber(@Param('ref_or_target_id') ref_or_target_id: string) {
    return this.otpVerificationService.getActiveOtp(ref_or_target_id);
  }
}

@ApiTags(`${MODULE_NAME.OTP_VERIFIER.split('-').join(' ')} - data`)
@Controller(`v1/${MODULE_NAME.OTP_VERIFIER}`)
@ApiBearerAuth('JWT')
@Public(false)
export class OtpVerifierController {
  constructor(private readonly otpVerifierService: OtpVerifierService) {}

  @Post()
  @ExcludePrivilege()
  async create(@Body() body: OtpVerifierCreateDto) {
    return await this.otpVerifierService.create(body);
  }
}
