# -*- coding: utf-8 -*-

from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError

class VisitLocationOtpWizard(models.TransientModel):
    _name = 'visit.location.otp.wizard'
    _description = 'Visit Location OTP Verification Wizard'

    def _default_visit(self):
        return self._context.get('active_id')

    otp_code = fields.Char(string="Enter OTP", required=True)
    visit_id = fields.Many2one('visit.location', string="Visit", default=_default_visit)
    visit_otp_code = fields.Char(string="Visit OTP", related="visit_id.otp_code")

    def action_confirm_otp(self):
        """Check OTP entered by user"""
        self.ensure_one()
        if self.otp_code != self.visit_id.otp_code:
            raise UserError(_("Invalid OTP. Please try again."))

        self.visit_id.write({'state': 'done',})
        self.visit_id.benefit_id.last_visit_date = self.visit_id.visit_date
        self.visit_id.action_send_survey()

class VisitSkipOtpWizard(models.TransientModel):
    _name = 'visit.skip.otp.wizard'
    _description = 'Skip OTP Reason Wizard'

    def _default_visit(self):
        return self._context.get('active_id')

    visit_id = fields.Many2one('visit.location', string="Visit", default=_default_visit)
    reason = fields.Text(string="Reason for Skipping OTP", required=True)

    def action_confirm_skip(self):
        self.ensure_one()
        visit = self.visit_id
        if not self.reason:
            raise UserError(_("You must provide a reason for skipping OTP verification."))
        visit.benefit_id.last_visit_date = visit.visit_date
        visit.write({'state': 'done'})
        visit.action_send_survey()
        visit.message_post(
            body=_("OTP verification skipped by %s.<br/>Reason: %s") % (self.env.user.name, self.reason),
            message_type='notification'
        )
        return {'type': 'ir.actions.act_window_close'}