# Fusion Accounting - File Download Error Wizard
# Displays generation errors and optionally offers a partial file download

from odoo import fields, models


class AccountReportFileDownloadErrorWizard(models.TransientModel):
    """Transient wizard shown when report file generation encounters
    recoverable errors, giving the user visibility into what went
    wrong and an option to download any partial output."""

    _name = 'account.report.file.download.error.wizard'
    _description = "Fusion Accounting Report Download Error Handler"

    actionable_errors = fields.Json()
    file_name = fields.Char()
    file_content = fields.Binary()

    def button_download(self):
        """Trigger browser download of the partially generated file,
        if one was produced despite the errors."""
        self.ensure_one()
        if self.file_name:
            download_url = (
                f'/web/content/{self._name}/{self.id}'
                f'/file_content/{self.file_name}?download=1'
            )
            return {
                'type': 'ir.actions.act_url',
                'url': download_url,
                'close': True,
            }
