"""
config_loader - 参数治理双通道加载器

职责：
  1. effective_params.json — 当前系统生效值（优先读取）
  2. suggested_params.json — Hyperopt 建议值（回退读取）

优先级：effective > suggested > 策略默认值

用法：
    from config_loader import load_effective_params, merge_params
    params = load_effective_params(project_root)
"""
import json
from pathlib import Path
from typing import Any, Dict, Optional


def load_json(path: Path) -> Dict[str, Any]:
    if not path.exists():
        return {}
    try:
        return json.loads(path.read_text(encoding="utf-8"))
    except Exception:
        return {}


def _effective_path(root: Path) -> Path:
    return root / "logs" / "effective_params.json"


def _suggested_path(root: Path) -> Path:
    return root / "logs" / "params_suggestion.json"


def load_effective_params(root: Path) -> Dict[str, Any]:
    """
    加载当前生效参数。
    优先读 effective_params.json，不存在则回退到 suggested_params.json。
    如果都没有，返回空 dict。
    """
    eff = _effective_path(root)
    sug = _suggested_path(root)

    if eff.exists():
        return load_json(eff)

    if sug.exists():
        return load_json(sug)

    return {}


def get_effective_value(
    root: Path,
    param_name: str,
    default: Any = None,
) -> Any:
    """
    获取单个参数值。
    优先级：effective > suggested > default
    """
    data = load_effective_params(root)
    suggestions = data.get("suggestions", [])

    for sug in suggestions:
        if sug.get("param") == param_name and sug.get("auto_apply"):
            return sug.get("suggested_value", default)
    return default


def build_effective_params_file(root: Path, approved_suggestions: list) -> Path:
    """
    将审核通过的 suggestions 写入 effective_params.json。
    这是"一键应用建议"时的写入接口。
    """
    eff = _effective_path(root)
    eff.parent.mkdir(parents=True, exist_ok=True)

    content = {
        "timestamp": _now_iso(),
        "source": "user_approved",
        "approved_count": len(approved_suggestions),
        "suggestions": approved_suggestions,
    }

    eff.write_text(json.dumps(content, ensure_ascii=False, indent=2), encoding="utf-8")
    return eff


def _now_iso() -> str:
    from datetime import datetime, timezone
    return datetime.now(timezone.utc).isoformat()


def diff_effective_vs_suggested(root: Path) -> Dict[str, Any]:
    """
    对比 effective 和 suggested 的差异，返回变更列表。
    供 GUI 显示"哪些参数待审核"。
    """
    eff_data = load_json(_effective_path(root))
    sug_data = load_json(_suggested_path(root))

    eff_params = {s["param"]: s for s in eff_data.get("suggestions", [])}
    sug_params = {s["param"]: s for s in sug_data.get("suggestions", [])}

    all_params = set(eff_params.keys()) | set(sug_params.keys())
    diffs = []

    for param in all_params:
        eff_val = eff_params.get(param, {}).get("suggested_value")
        sug_val = sug_params.get(param, {}).get("suggested_value")
        if eff_val != sug_val:
            diffs.append({
                "param": param,
                "effective_value": eff_val,
                "suggested_value": sug_val,
                "suggested_reason": sug_params.get(param, {}).get("reason", ""),
            })

    return {
        "effective_timestamp": eff_data.get("timestamp"),
        "suggested_timestamp": sug_data.get("timestamp"),
        "diff_count": len(diffs),
        "diffs": diffs,
    }
