# Fusion Accounting - Account extensions
# Provides reconciliation actions, asset automation, and budget linkage

import ast

from odoo import api, fields, models, _


class FusionAccountAccount(models.Model):
    """Augments the standard chart of accounts with asset management
    and reconciliation capabilities for Fusion Accounting."""

    _inherit = "account.account"

    # ---- Relational Fields ----
    exclude_provision_currency_ids = fields.Many2many(
        comodel_name='res.currency',
        relation='account_account_exclude_res_currency_provision',
        help="Currencies excluded from foreign exchange provision calculations.",
    )
    budget_item_ids = fields.One2many(
        comodel_name='account.report.budget.item',
        inverse_name='account_id',
    )
    asset_model_ids = fields.Many2many(
        comodel_name='account.asset',
        domain=[('state', '=', 'model')],
        help="When this account appears on a vendor bill or credit note, "
             "an asset record is generated per linked model.",
        tracking=True,
    )

    # ---- Selection & Computed ----
    create_asset = fields.Selection(
        selection=[
            ('no', 'No'),
            ('draft', 'Create in draft'),
            ('validate', 'Create and validate'),
        ],
        required=True,
        default='no',
        tracking=True,
    )
    can_create_asset = fields.Boolean(
        compute="_compute_asset_creation_eligible",
    )
    form_view_ref = fields.Char(
        compute='_compute_asset_creation_eligible',
    )
    multiple_assets_per_line = fields.Boolean(
        string='Multiple Assets per Line',
        default=False,
        tracking=True,
        help="Generate individual asset records per unit quantity on "
             "the bill line rather than a single consolidated asset.",
    )

    # ---- Compute Methods ----
    @api.depends('account_type')
    def _compute_asset_creation_eligible(self):
        """Determine whether the account type supports automatic
        asset creation and set the appropriate form view reference."""
        eligible_types = ('asset_fixed', 'asset_non_current')
        for acct in self:
            acct.can_create_asset = acct.account_type in eligible_types
            acct.form_view_ref = 'fusion_accountingview_account_asset_form'

    # ---- Onchange ----
    @api.onchange('create_asset')
    def _onchange_reset_multiple_assets(self):
        """Disable per-line asset splitting when asset creation is turned off."""
        for acct in self:
            if acct.create_asset == 'no':
                acct.multiple_assets_per_line = False

    # ---- Actions ----
    def action_open_reconcile(self):
        """Navigate to unreconciled journal items filtered by this account."""
        self.ensure_one()
        act_data = self.env['ir.actions.act_window']._for_xml_id(
            'fusion_accounting.action_move_line_posted_unreconciled'
        )
        parsed_domain = ast.literal_eval(act_data.get('domain', '[]'))
        parsed_domain.append(('account_id', '=', self.id))
        act_data['domain'] = parsed_domain
        return act_data
