# -*- 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'

    # =========================================================================
    # AI SETTINGS
    # =========================================================================
    x_fa_ai_enabled = fields.Boolean(
        string='Enable AI Extraction',
        config_parameter='fusion_accounts.ai_enabled',
        help='Enable AI-powered data extraction from email body and attachments.',
    )
    x_fa_openai_api_key = fields.Char(
        string='OpenAI API Key',
        config_parameter='fusion_accounts.openai_api_key',
        help='Your OpenAI API key for bill data extraction.',
    )
    x_fa_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_accounts.ai_model',
        help='OpenAI model to use for extraction.',
    )
    x_fa_ai_max_pages = fields.Integer(
        string='Max PDF Pages',
        config_parameter='fusion_accounts.ai_max_pages',
        help='Maximum number of PDF pages to send to AI for extraction.',
    )

    # =========================================================================
    # MATCHING SETTINGS
    # =========================================================================
    x_fa_enable_domain_match = fields.Boolean(
        string='Enable Domain Matching',
        config_parameter='fusion_accounts.enable_domain_match',
        help='Match vendors by email domain (Level 2 matching).',
    )
    x_fa_enable_name_match = fields.Boolean(
        string='Enable Name Matching',
        config_parameter='fusion_accounts.enable_name_match',
        help='Match vendors by sender display name (Level 3 matching).',
    )
    x_fa_auto_block_po_vendors = fields.Boolean(
        string='Auto-Block PO Vendors',
        config_parameter='fusion_accounts.auto_block_po_vendors',
        help='Automatically block email bill creation for vendors with active Purchase Orders.',
    )

    # =========================================================================
    # GENERAL SETTINGS
    # =========================================================================
    x_fa_log_retention_days = fields.Integer(
        string='Log Retention (Days)',
        config_parameter='fusion_accounts.log_retention_days',
        help='Number of days to keep activity logs. Set 0 to keep forever.',
    )

    def set_values(self):
        ICP = self.env['ir.config_parameter'].sudo()
        # Protect API key and customized settings from accidental blanking
        _protected = {
            'fusion_accounts.openai_api_key': ICP.get_param('fusion_accounts.openai_api_key', ''),
            'fusion_accounts.ai_model': ICP.get_param('fusion_accounts.ai_model', ''),
            'fusion_accounts.ai_max_pages': ICP.get_param('fusion_accounts.ai_max_pages', ''),
        }
        super().set_values()
        for key, old_val in _protected.items():
            new_val = ICP.get_param(key, '')
            if not new_val and old_val:
                ICP.set_param(key, old_val)
                _logger.warning("Settings protection: restored %s", key)
