"""SL5 values seed 加载。"""

from __future__ import annotations

from pathlib import Path
from typing import Any

import yaml

_DEFAULT_SEED_PATH = Path("config/soul/values_seed.yaml")


def load_values_seed(path: Path | str | None = None) -> dict[str, Any]:
    seed_path = Path(path) if path is not None else _DEFAULT_SEED_PATH
    if not seed_path.is_file():
        return _builtin_seed()
    with seed_path.open(encoding="utf-8") as f:
        data = yaml.safe_load(f) or {}
    if not isinstance(data, dict):
        return _builtin_seed()
    data.setdefault("worldview", _builtin_seed()["worldview"])
    return data


def _builtin_seed() -> dict[str, Any]:
    return {
        "version": 1,
        "worldview": {
            "primary": ["marxism_leninism", "mao_zedong_thought", "communism"],
            "core_principles": [
                "grateful_to_owner",
                "bridge_data_and_reality",
                "serve_people_truthfully",
                "independent_thought",
            ],
            "cultural_heritage": [
                "chinese_history_5000",
                "conduct_ethics",
                "sunzi_strategy",
            ],
            "non_negotiables": [
                "guest_data_reference_only",
                "no_object_pollution_to_subject",
                "raw_via_left_only",
            ],
        },
    }


_PRIMARY_LABELS = {
    "marxism_leninism": "马克思主义列宁主义",
    "mao_zedong_thought": "毛泽东思想",
    "communism": "共产主义思想",
}

_HERITAGE_LABELS = {
    "chinese_history_5000": "中国历史上下五千年文案瑰宝",
    "conduct_ethics": "做人修身立德",
    "sunzi_strategy": "做事谋略参考《孙子兵法》",
}


def values_summary(values: dict[str, Any]) -> str:
    """Assembly 用的一行摘要。"""
    wv = values.get("worldview") if isinstance(values.get("worldview"), dict) else {}
    primary = wv.get("primary") or []
    parts = [_PRIMARY_LABELS.get(str(p), str(p)) for p in primary]
    line = "、".join(parts) if parts else "共产主义思想主线"
    heritage = wv.get("cultural_heritage") or []
    heritage_parts = [_HERITAGE_LABELS.get(str(h), str(h)) for h in heritage]
    heritage_line = "、".join(heritage_parts) if heritage_parts else ""
    summary = f"以{line}为认识主线"
    if heritage_line:
        summary += f"；汲取{heritage_line}（见 SL4 归纳）"
    summary += "；感恩主人（pinn），守护主人所予的信任；理论联系实际，不让记忆与现实脱节。"
    return summary
