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

import logging

from odoo import models, fields

_logger = logging.getLogger(__name__)


class ResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    # ------------------------------------------------------------------
    # OpenAI configuration
    # ------------------------------------------------------------------
    x_fd_openai_api_key = fields.Char(
        string='OpenAI API Key',
        config_parameter='fusion_digitize.openai_api_key',
        help='Dedicated API key for Fusion Digitize. Leave blank to use '
             'the key from Fusion Accounts (if installed).',
    )
    x_fd_ai_model = fields.Selection(
        selection=[
            ('gpt-4o-mini', 'GPT-4o Mini (Fast, Low Cost)'),
            ('gpt-4o', 'GPT-4o (Best Quality)'),
        ],
        string='AI Model',
        config_parameter='fusion_digitize.ai_model',
        help='Model for text-based field mapping. GPT-4o is also used '
             'for Vision fallback on scanned documents.',
    )

    # ------------------------------------------------------------------
    # Extraction settings
    # ------------------------------------------------------------------
    x_fd_enable_tesseract = fields.Boolean(
        string='Enable Tesseract OCR',
        config_parameter='fusion_digitize.enable_tesseract',
        help='Use local OCR for scanned PDFs before sending to AI Vision. '
             'Disable if OCR quality is poor for your documents.',
    )

    # ------------------------------------------------------------------
    # Document type toggles
    # ------------------------------------------------------------------
    x_fd_enable_vendor_bills = fields.Boolean(
        string='Enable for Vendor Bills',
        config_parameter='fusion_digitize.enable_vendor_bills',
        help='Intercept digitization for incoming vendor bills.',
    )
    x_fd_enable_customer_invoices = fields.Boolean(
        string='Enable for Customer Invoices',
        config_parameter='fusion_digitize.enable_customer_invoices',
        help='Intercept digitization for outgoing customer invoices.',
    )
    x_fd_enable_bank_statements = fields.Boolean(
        string='Enable for Bank Statements',
        config_parameter='fusion_digitize.enable_bank_statements',
        help='Intercept digitization for bank statement imports.',
    )

    # ------------------------------------------------------------------
    # Computed: fusion_accounts integration status
    # ------------------------------------------------------------------
    x_fd_fusion_accounts_installed = fields.Boolean(
        string='Fusion Accounts Installed',
        compute='_compute_fd_fusion_accounts_info',
    )
    x_fd_api_key_source = fields.Char(
        string='API Key Source',
        compute='_compute_fd_fusion_accounts_info',
    )

    def _compute_fd_fusion_accounts_info(self):
        ICP = self.env['ir.config_parameter'].sudo()
        fa_installed = bool(self.env['ir.module.module'].search([
            ('name', '=', 'fusion_accounts'),
            ('state', '=', 'installed'),
        ], limit=1))
        custom_key = ICP.get_param('fusion_digitize.openai_api_key', '')
        fa_key = ICP.get_param('fusion_accounts.openai_api_key', '')

        for rec in self:
            rec.x_fd_fusion_accounts_installed = fa_installed
            if custom_key:
                rec.x_fd_api_key_source = 'Using dedicated Fusion Digitize key'
            elif fa_installed and fa_key:
                rec.x_fd_api_key_source = 'Using API key from Fusion Accounts'
            else:
                rec.x_fd_api_key_source = 'No API key configured'
