import { OtpService } from '../../services/otpservice.js'
import { sendError } from '../../utils/response.js'
import { GeneralService } from '../../share/generalservice.js'

export class OtpController {
  constructor() {
    this.otpService = new OtpService()
    this.generalService = new GeneralService()
  }

  async onSendOtp(req, res) {
    let idx = -1, result = []
    try {
      const email  = req.body.request.email
      result = await this.otpService.sendOtp(email)
      this.generalService.devhint(1, 'otpcontroller.js', `OTP sent to ${email} OTP: ${result.otp}`)
    } catch (error) {
      idx = 1
      this.generalService.devhint(1, 'otpcontroller.js', 'Jumpout Detected', error.message)
    } finally {
      delete result.otp
      if (idx === 1) return sendError('ไม่สามารถส่ง OTP ได้', 'Send failed')
      return result
    }
  }
}
