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

from odoo import models, fields, api, _


class ReasonForReturnWizard(models.TransientModel):
    _name = 'reason.for.return.wizard'
    _description = 'Reason For Return Wizard'

    reason = fields.Text(string="reason for return", required=True)

    def action_confirm(self):
        active_id = self._context.get('active_id')
        active_model = self._context.get('active_model')

        if active_model == 'service.request':
            self.env[active_model].browse(active_id).write({
                'return_reason': self.reason,
                'state': 'researcher',
            })
        elif active_model == 'confirm.benefit.expense':
            expense = self.env[active_model].browse(active_id)

            message = _(
                '<b>State Changed:</b> %s → Calculated<br/>'
                '<b>Reset By:</b> %s<br/>'
                '<b>Reason:</b> %s'
            ) % (dict(expense.fields_get(['state'])['state']['selection']).get(expense.state, expense.state),
                 self.env.user.name, self.reason)
            expense.sudo().message_post(
                body=message,
                subject=_('Benefit Expense Reset'),
                message_type='notification', )

            if expense.payment_order_id:
                expense.sudo().payment_order_id.unlink()
            if expense.move_id:
                expense.sudo().move_id.unlink()
            expense.sudo().state = 'calculated'
        elif active_model == 'seasonal.service':
            seasonal = self.env[active_model].browse(active_id)
            old_state = dict(seasonal.fields_get(['state'])['state']['selection']).get(seasonal.state, seasonal.state)
            message = _(
                "<b>State Changed:</b> %s → Calculated<br/>"
                "<b>Returned By:</b> %s<br/>"
                "<b>Reason:</b> %s"
            ) % (old_state, self.env.user.name, self.reason)

            seasonal.sudo().message_post(
                body=message,
                message_type='notification',
            )
            seasonal.sudo().service_requests_ids.write({'state': 'draft'})
            seasonal.sudo().payment_order_id.unlink()
            seasonal.sudo().vendor_bill.unlink()
            seasonal.sudo().state = 'calculated'

        return {'type': 'ir.actions.act_window_close'}


class ReturnReasonWizard(models.TransientModel):
    _name = "return.reason.wizard"
    _description = "Return Reason Wizard"

    line_id = fields.Integer("Line ID")
    line_model = fields.Char("Line Model")
    return_reason_id = fields.Many2one("return.reason", string="Return Reason", required=True)

    def action_confirm(self):
        self.ensure_one()

        record = self.env[self.line_model].browse(self.line_id)
        record.return_reason_id = self.return_reason_id.id
        if hasattr(record, "is_return"):
            record.is_return = True
        if self.line_model == "service.request" and hasattr(record, "state"):
            record.state = "return_to_bank"
            record.payment_order_id = False

        return {'type': 'ir.actions.act_window_close'}
