const { Admin, AdminOTP } = require("../../models");
const { BadRequestError } = require("../../utils/errors");


class OTPService {
   async generateOTP(adminId ,type,  ip, device){
const admin =  await Admin.findOne({
    where: {
        id: adminId
    }
})
   const otp =  await AdminOTP.createOTP({
    adminId,
    type,
    identifier: admin.email,
    expiryMinutes: 20,
    ip,
    device
   })
    return {
        message: `Otp send to ${admin.email}`,
        otp
    }
   }

   async verifyOTP(adminId , type, code ) {
    
    // Find latest valid OTP
    const otp = await AdminOTP.findValidOTP(adminId , type, code);

    if (!otp) {
      throw new Error('Invalid or expired OTP');
    }

    // Mark as used
    await otp.verify(code);

    return {
      status: true,
    };
  }
}

module.exports = new OTPService()