import { sendError } from '../../utils/response.js';
import { OTPVerifyService } from '../../services/otpverifyservice.js';
import { GeneralService } from '../../share/generalservice.js';

export class OtpVerifyController {
  constructor() {
    this.otpVerifyService = new OTPVerifyService();
    this.generalService = new GeneralService();
  }

  async onNavigate(req, res) {
    let idx = -1, result = [];
    try {
      const { email, otp } = req.body.request;
      result = await this.otpVerifyService.verifyOtp(email, otp);
    } catch (error) {
      idx = 1;
      this.generalService.devhint(1, 'otpverifycontroller.js', 'Jumpout', error.message);
    } finally {
      if (idx === 1) return sendError('เกิดข้อผิดพลาดในการตรวจสอบ OTP', 'OTP verification error');
      return result;
    }
  }
}
