#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
AI准确率配置
管理AI建议的准确率阈值和相关配置
"""

from dataclasses import dataclass
from typing import Dict, Any

@dataclass
class AIAccuracyConfig:
    """AI准确率配置类"""
    
    # 相似度阈值配置
    auto_approve_threshold: float = 0.95  # 自动审批阈值（≥95%）
    use_human_resolution_threshold: float = 0.90  # 使用人工描述阈值（<90%）
    manual_review_threshold: float = 0.80  # 人工审核阈值（≥80%）
    
    # 置信度配置
    ai_suggestion_confidence: float = 0.95  # AI建议默认置信度
    human_resolution_confidence: float = 0.90  # 人工描述置信度
    
    # 入库策略配置
    prefer_human_when_low_accuracy: bool = True  # 当AI准确率低时优先使用人工描述
    enable_auto_approval: bool = True  # 是否启用自动审批
    enable_human_fallback: bool = True  # 是否启用人工描述回退
    
    def get_threshold_explanation(self, similarity: float) -> str:
        """获取相似度阈值的解释"""
        if similarity >= self.auto_approve_threshold:
            return f"相似度≥{self.auto_approve_threshold*100:.0f}%，自动审批使用AI建议"
        elif similarity >= self.manual_review_threshold:
            return f"相似度≥{self.manual_review_threshold*100:.0f}%，建议人工审核"
        elif similarity >= self.use_human_resolution_threshold:
            return f"相似度<{self.use_human_resolution_threshold*100:.0f}%，建议使用人工描述"
        else:
            return f"相似度<{self.use_human_resolution_threshold*100:.0f}%，优先使用人工描述"
    
    def should_use_human_resolution(self, similarity: float) -> bool:
        """判断是否应该使用人工描述"""
        return similarity < self.use_human_resolution_threshold
    
    def should_auto_approve(self, similarity: float) -> bool:
        """判断是否应该自动审批"""
        return similarity >= self.auto_approve_threshold and self.enable_auto_approval
    
    def get_confidence_score(self, similarity: float, use_human: bool = False) -> float:
        """获取置信度分数"""
        if use_human:
            return self.human_resolution_confidence
        else:
            return max(similarity, self.ai_suggestion_confidence)
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return {
            "auto_approve_threshold": self.auto_approve_threshold,
            "use_human_resolution_threshold": self.use_human_resolution_threshold,
            "manual_review_threshold": self.manual_review_threshold,
            "ai_suggestion_confidence": self.ai_suggestion_confidence,
            "human_resolution_confidence": self.human_resolution_confidence,
            "prefer_human_when_low_accuracy": self.prefer_human_when_low_accuracy,
            "enable_auto_approval": self.enable_auto_approval,
            "enable_human_fallback": self.enable_human_fallback
        }
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'AIAccuracyConfig':
        """从字典创建配置"""
        return cls(**data)

# 默认配置实例
DEFAULT_CONFIG = AIAccuracyConfig()

# 配置预设
PRESETS = {
    "conservative": AIAccuracyConfig(
        auto_approve_threshold=0.98,
        use_human_resolution_threshold=0.85,
        manual_review_threshold=0.90,
        human_resolution_confidence=0.95
    ),
    "balanced": AIAccuracyConfig(
        auto_approve_threshold=0.95,
        use_human_resolution_threshold=0.90,
        manual_review_threshold=0.80,
        human_resolution_confidence=0.90
    ),
    "aggressive": AIAccuracyConfig(
        auto_approve_threshold=0.90,
        use_human_resolution_threshold=0.80,
        manual_review_threshold=0.70,
        human_resolution_confidence=0.85
    )
}

def get_accuracy_config(preset: str = "balanced") -> AIAccuracyConfig:
    """获取准确率配置"""
    return PRESETS.get(preset, DEFAULT_CONFIG)

def update_accuracy_config(config: AIAccuracyConfig) -> bool:
    """更新准确率配置（可以保存到文件或数据库）"""
    try:
        # 这里可以实现配置的持久化存储
        # 例如保存到配置文件或数据库
        return True
    except Exception:
        return False
