# Fusion Accounting - Bank Setup Wizard Extension
# Automatically configures file import as the bank statement source

from odoo import models


class SetupBarBankConfigWizard(models.TransientModel):
    """Extends the manual bank configuration wizard to default newly
    created bank journals to use file-based statement imports when
    supported import formats are available."""

    _inherit = 'account.setup.bank.manual.config'

    def validate(self):
        """After standard bank validation, check whether the journal
        should use file import as its statement source."""
        result = super().validate()

        is_fresh_journal = self.num_journals_without_account == 0
        has_undefined_source = self.linked_journal_id.bank_statements_source == 'undefined'
        available_formats = (
            self.env['account.journal']._get_bank_statements_available_import_formats()
        )

        if (is_fresh_journal or has_undefined_source) and available_formats:
            self.linked_journal_id.bank_statements_source = 'file_import'

        return result
