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

import logging

from odoo import models

_logger = logging.getLogger(__name__)


class AccountBankStatement(models.Model):
    _inherit = 'account.bank.statement'

    def _contact_iap_extract(self, pathinfo, params):
        ICP = self.env['ir.config_parameter'].sudo()
        service = self.env['fusion.digitize.service']

        api_key = service._get_api_key()
        if not api_key:
            return super()._contact_iap_extract(pathinfo, params)

        enabled = ICP.get_param(
            'fusion_digitize.enable_bank_statements', 'True',
        ) == 'True'
        if not enabled:
            return super()._contact_iap_extract(pathinfo, params)

        if pathinfo == 'parse':
            _logger.info(
                "Fusion Digitize: intercepting bank statement parse request",
            )
            return service._handle_parse(params, 'bank_statement')

        if pathinfo == 'get_result':
            return service._handle_get_result(params)

        if pathinfo == 'validate':
            return {'status': 'success'}

        return super()._contact_iap_extract(pathinfo, params)
