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

from odoo import api, fields, models


class AccountMove(models.Model):
    _inherit = 'account.move'

    x_ff_fax_ids = fields.One2many(
        'fusion.fax',
        'account_move_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 move in self:
            move.x_ff_fax_count = len(move.x_ff_fax_ids)

    def action_send_fax(self):
        """Open the Send Fax wizard pre-filled with this invoice."""
        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': 'account.move',
                'active_id': self.id,
            },
        }

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