# -*- coding: utf-8 -*-
# Copyright 2024-2026 Nexa Systems Inc.
# License OPL-1 (Odoo Proprietary License v1.0)

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


class SaleOrder(models.Model):
    _inherit = 'sale.order'

    x_fc_loaner_checkout_ids = fields.One2many(
        'fusion.loaner.checkout',
        'sale_order_id',
        string='Loaner Checkouts',
        help='Loaner equipment checked out for this order',
    )
    x_fc_loaner_count = fields.Integer(
        string='Loaners',
        compute='_compute_loaner_count',
    )
    x_fc_active_loaner_count = fields.Integer(
        string='Active Loaners',
        compute='_compute_loaner_count',
    )
    x_fc_has_overdue_loaner = fields.Boolean(
        string='Has Overdue Loaner',
        compute='_compute_loaner_count',
    )

    @api.depends('x_fc_loaner_checkout_ids', 'x_fc_loaner_checkout_ids.state',
                 'x_fc_loaner_checkout_ids.expected_return_date')
    def _compute_loaner_count(self):
        today = fields.Date.today()
        for order in self:
            active = order.x_fc_loaner_checkout_ids.filtered(
                lambda l: l.state in ('checked_out', 'overdue', 'rental_pending')
            )
            order.x_fc_loaner_count = len(order.x_fc_loaner_checkout_ids)
            order.x_fc_active_loaner_count = len(active)
            order.x_fc_has_overdue_loaner = any(
                l.state == 'overdue' or (l.expected_return_date and l.expected_return_date < today)
                for l in active
            )

    def action_view_loaners(self):
        self.ensure_one()
        action = {
            'name': 'Loaner Checkouts',
            'type': 'ir.actions.act_window',
            'res_model': 'fusion.loaner.checkout',
            'view_mode': 'tree,form',
            'domain': [('sale_order_id', '=', self.id)],
            'context': {'default_sale_order_id': self.id},
        }
        if len(self.x_fc_loaner_checkout_ids) == 1:
            action['view_mode'] = 'form'
            action['res_id'] = self.x_fc_loaner_checkout_ids.id
        return action

    def action_checkout_loaner(self):
        self.ensure_one()
        ctx = {
            'default_sale_order_id': self.id,
            'default_partner_id': self.partner_id.id,
        }
        if hasattr(self, 'x_fc_authorizer_id') and self.x_fc_authorizer_id:
            ctx['default_authorizer_id'] = self.x_fc_authorizer_id.id
        return {
            'name': 'Checkout Loaner',
            'type': 'ir.actions.act_window',
            'res_model': 'fusion.loaner.checkout.wizard',
            'view_mode': 'form',
            'target': 'new',
            'context': ctx,
        }

    def action_checkin_loaner(self):
        self.ensure_one()
        active_loaners = self.x_fc_loaner_checkout_ids.filtered(
            lambda l: l.state in ('checked_out', 'overdue', 'rental_pending')
        )
        if not active_loaners:
            raise UserError("No active loaners to check in for this order.")
        if len(active_loaners) == 1:
            return active_loaners.action_return()
        return {
            'name': 'Return Loaner',
            'type': 'ir.actions.act_window',
            'res_model': 'fusion.loaner.checkout',
            'view_mode': 'tree,form',
            'domain': [('id', 'in', active_loaners.ids)],
            'target': 'current',
        }
