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

from odoo import api, fields, models


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

    x_ff_fax_ids = fields.One2many(
        'fusion.fax',
        'sale_order_id',
        string='Faxes',
    )
    x_ff_fax_count = fields.Integer(
        string='Fax Count',
        compute='_compute_fax_count',
    )

    @api.depends('x_ff_fax_ids')
    def _compute_fax_count(self):
        for order in self:
            order.x_ff_fax_count = len(order.x_ff_fax_ids)

    def action_send_fax(self):
        """Open the Send Fax wizard pre-filled with this sale order."""
        self.ensure_one()
        return {
            'type': 'ir.actions.act_window',
            'name': 'Send Fax',
            'res_model': 'fusion_faxes.send.fax.wizard',
            'view_mode': 'form',
            'target': 'new',
            'context': {
                'active_model': 'sale.order',
                'active_id': self.id,
            },
        }

    def action_view_faxes(self):
        """Open fax history for this sale order."""
        self.ensure_one()
        return {
            'type': 'ir.actions.act_window',
            'name': 'Faxes',
            'res_model': 'fusion.fax',
            'view_mode': 'list,form',
            'domain': [('sale_order_id', '=', self.id)],
            'context': {'default_sale_order_id': self.id},
        }
