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

from odoo import api, fields, models


class SendFaxWizardLine(models.TransientModel):
    _name = 'fusion_faxes.send.fax.wizard.line'
    _description = 'Send Fax Wizard Document Line'
    _order = 'sequence, id'

    wizard_id = fields.Many2one(
        'fusion_faxes.send.fax.wizard',
        string='Wizard',
        required=True,
        ondelete='cascade',
    )
    sequence = fields.Integer(
        string='Order',
        default=10,
    )
    file_upload = fields.Binary(
        string='Upload File',
    )
    file_name = fields.Char(
        string='File Name',
    )
    attachment_id = fields.Many2one(
        'ir.attachment',
        string='Attachment',
        readonly=True,
    )

    @api.onchange('file_upload')
    def _onchange_file_upload(self):
        """Create an ir.attachment when a file is uploaded."""
        if self.file_upload and self.file_name:
            attachment = self.env['ir.attachment'].create({
                'name': self.file_name,
                'type': 'binary',
                'datas': self.file_upload,
                'res_model': 'fusion.fax',
            })
            self.attachment_id = attachment.id
