import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { OtpService } from './otp.service';
import { CreateOtpDto } from './dto/create-otp.dto';
import { UpdateOtpDto } from './dto/update-otp.dto';
import { NotifyService } from 'src/services/notify/notify.service';
import { Utilisateur } from '../utilisateurs/entities/utilisateur.entity';

@Controller('otp')
export class OtpController {
  constructor(private readonly otpService: OtpService,/*  private readonly notifyService: NotifyService */) {}

  @Post('generate')
  async requestOtp(@Body('userId') userId: Utilisateur) {
    const otp = await this.otpService.generateOtp(userId);
    
    const otpCode = otp.code;
    const contenu = `
    Bonjour <b>${otp.user.nom.toLocaleUpperCase()} ${otp.user.prenoms}</b>,
    <br><br>
    Vous avez récemment demandé à modifier votre mot de passe.
    <br>
    Veuillez utiliser le code de vérification ci-dessous pour confirmer votre identité :
    <br><br>
    Code de vérification : <b>${otpCode}</b>
     <br><br>
    Ce code est valable pendant 5 minutes. Ne le partagez avec personne.
      <br><br>
    Si vous n'êtes pas à l'origine de cette demande, veuillez ignorer ce message ou contacter notre support immédiatement.
    <br>
    Cordialement, 
    <br><br> 
    Orange Bank Africa
    `;
    //Envoi de l'otp par mail
    //await this.notifyService.sendPostRequest('moise.deki@orangebank.ci', contenu, 'Code de vérification pour la modification de votre mot de passe');
    return { message: 'OTP généré et envoyé'};
  }

  @Post('verify')
  async verifyOtp(@Body() body: { userId: number; code: string }) {

    const result = await this.otpService.verifyOtp(body.userId, body.code);
    return { message: result ? 'OTP vérifié' : 'Échec de la vérification' };
  }

  @Post()
  create(@Body() createOtpDto: CreateOtpDto) {
    return this.otpService.create(createOtpDto);
  }

  @Get()
  findAll() {
    return this.otpService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.otpService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateOtpDto: UpdateOtpDto) {
    return this.otpService.update(+id, updateOtpDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.otpService.remove(+id);
  }
}
