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

import logging

from odoo import api, models

_logger = logging.getLogger(__name__)


class AccountMove(models.Model):
    _inherit = 'account.move'

    @api.model
    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)

        vendor_bills_enabled = ICP.get_param(
            'fusion_digitize.enable_vendor_bills', 'True',
        ) == 'True'
        customer_invoices_enabled = ICP.get_param(
            'fusion_digitize.enable_customer_invoices', 'True',
        ) == 'True'

        if not vendor_bills_enabled and not customer_invoices_enabled:
            return super()._contact_iap_extract(pathinfo, params)

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

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

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

        return super()._contact_iap_extract(pathinfo, params)
