"""Streamlit 面板文案与五脏显示名（中英文）。"""
from __future__ import annotations

from typing import Any

from .locale_config import LANG_EN, LANG_ZH

BODY_DISPLAY_ZH: dict[str, str] = {
    "liver": "肝 (liver)",
    "heart": "心 (heart)",
    "spleen": "脾 (spleen)",
    "lung": "肺 (lung)",
    "kidney": "肾 (kidney)",
}

BODY_DISPLAY_EN: dict[str, str] = {
    "liver": "Liver",
    "heart": "Heart",
    "spleen": "Spleen",
    "lung": "Lung",
    "kidney": "Kidney",
}


def body_label(key: str, lang: str) -> str:
    if lang == LANG_ZH:
        return BODY_DISPLAY_ZH.get(key, key)
    return BODY_DISPLAY_EN.get(key, key)


STRINGS: dict[str, dict[str, Any]] = {
    LANG_EN: {
        "page_title": "Body emotion · debug panel",
        "main_title": "Body-axis persistent state · live monitor",
        "language_label": "Language",
        "state_path": "State file path",
        "load": "Load",
        "save": "Save",
        "reset_default": "Reset",
        "reset_ok": "State reset in memory (not saved to disk)",
        "loaded": "Loaded",
        "saved": "Saved",
        "file_missing": "File does not exist",
        "live_monitor": "Live monitor",
        "auto_refresh": "Auto-refresh",
        "auto_refresh_desc": "Check state/history files every 2 seconds.",
        "no_state_file": "State file missing; cannot monitor.",
        "check_file": "Check for changes",
        "changed_reload": "Change detected; state reloaded",
        "unchanged": "No change",
        "auto_changed": "Auto-detected change; state reloaded",
        "body_current": "Body axes · current",
        "delta_baseline": "Baseline",
        "fragility_caption": "fragility={fragility}",
        "body_meta_line": "baseline={baseline} | fragility={fragility}",
        "turn_delta_up": "This turn +{delta}",
        "turn_delta_down": "This turn -{delta}",
        "turn_delta_flat": "This turn 0",
        "full_params": "Full parameter view",
        "body_full": "Body axes (full)",
        "col_body": "axis",
        "col_name": "name",
        "col_baseline": "baseline",
        "col_current": "current",
        "col_turn_delta": "turn_delta",
        "col_turn_score": "turn_score",
        "col_fragility": "fragility",
        "col_delta_from_base": "delta_from_baseline",
        "col_before": "before",
        "col_after": "after",
        "col_stimulus_raw": "stimulus_raw",
        "col_stimulus_scaled": "stimulus_scaled",
        "col_reaction_raw": "reaction_raw",
        "col_reaction_scaled": "reaction_scaled",
        "col_total_raw": "total_raw",
        "col_total_scaled": "total_scaled",
        "col_current_offset": "current_offset",
        "col_phase_power": "phase_power",
        "col_generates": "generates",
        "col_constrains": "constrains",
        "col_phase_delta": "phase_delta",
        "col_recovery_delta": "recovery_delta",
        "col_persistent_delta": "persistent_delta",
        "col_saturated_delta": "saturated_delta",
        "traits_profile": "Traits / profile / last snapshot",
        "latest_snapshot": "Current prompt snapshot",
        "history_title": "Turn list",
        "history_empty": "No history yet.",
        "history_item_title": "Turn {index} · {target} · {context}",
        "history_tags_label": "tags",
        "history_selected": "Selected",
        "history_turn_tags_short": "Turn tags",
        "history_body_tag_short": "Body",
        "recent_input": "Latest input parameters",
        "body_turn": "Body axes · turn scores & current",
        "recent_output": "Latest prompt output",
        "history_json": "Full latest history JSON",
        "selected_turn_detail": "Selected turn detail",
        "trace_overview": "This view shows the complete numeric pipeline of the selected turn.",
        "trace_missing": "This turn does not contain trace data yet.",
        "trace_input": "1. Input",
        "trace_input_summary": "Input summary",
        "trace_context_focus": "Focus",
        "trace_base_mapping": "2. Base mapping and fragility scaling",
        "trace_base_mapping_table": "Per-axis raw/scaled totals",
        "trace_mapping_steps": "Detailed mapping steps",
        "trace_stimulus_steps": "Stimulus steps",
        "trace_reaction_steps": "Reaction steps",
        "trace_phase": "3. Phase propagation",
        "trace_phase_snapshot": "Frozen phase snapshot",
        "trace_phase_actions": "Source axis decisions",
        "trace_phase_result": "Phase delta by target axis",
        "trace_persistent": "4. Persistent update",
        "trace_persistent_table": "Current update detail",
        "trace_output": "5. Prompt output",
        "trace_output_tags": "Turn tags",
        "trace_output_body": "Current BODY_TAG",
        "trace_output_baseline_persona": "Baseline persona",
        "trace_output_compare": "Baseline vs current",
        "manual_section": "Paste structured emotion analysis JSON (AnalysisInput) for a trial run",
        "json_label": "JSON",
        "json_placeholder": '{"analysis_target": "...", ...}',
        "apply_turn": "Apply turn",
        "json_err": "JSON parse error",
        "process_err": "Validation or processing failed",
        "manual_result": "Manual trial result",
        "manual_input": "Input parameters",
        "manual_output": "Prompt output",
        "turn_tags_label": "TURN_CHANGE_TAGS",
        "body_tag_label": "BODY_TAG",
        "full_mapping_json": "Full MappingResult JSON",
    },
    LANG_ZH: {
        "page_title": "五脏情绪调试面板",
        "main_title": "五脏持久状态 · 实时监控面板",
        "language_label": "语言 / Language",
        "state_path": "state 文件路径",
        "load": "加载",
        "save": "保存",
        "reset_default": "重置默认",
        "reset_ok": "已重置内存中的状态（未自动写盘）",
        "loaded": "已加载",
        "saved": "已保存",
        "file_missing": "文件不存在",
        "live_monitor": "实时监控",
        "auto_refresh": "自动刷新",
        "auto_refresh_desc": "每 2 秒检查一次 state/history 文件变化。",
        "no_state_file": "当前 state 文件不存在，无法监控。",
        "check_file": "检查文件变化",
        "changed_reload": "检测到变化，已重新加载 state",
        "unchanged": "文件未变化",
        "auto_changed": "自动检测到变化，已重新加载 state",
        "body_current": "五脏 · 当前值",
        "delta_baseline": "基线",
        "fragility_caption": "脆弱度={fragility}",
        "body_meta_line": "基线={baseline} | 脆弱度={fragility}",
        "turn_delta_up": "本轮上升 {delta}",
        "turn_delta_down": "本轮下降 {delta}",
        "turn_delta_flat": "本轮无变化",
        "full_params": "全参数视图",
        "body_full": "五脏完整参数",
        "col_body": "脏腑",
        "col_name": "名称",
        "col_baseline": "基线",
        "col_current": "当前",
        "col_turn_delta": "本轮变化",
        "col_turn_score": "本轮得分",
        "col_fragility": "脆弱度",
        "col_delta_from_base": "相对基线",
        "col_before": "变化前",
        "col_after": "变化后",
        "col_stimulus_raw": "刺激原始分",
        "col_stimulus_scaled": "刺激缩放后",
        "col_reaction_raw": "反应原始分",
        "col_reaction_scaled": "反应缩放后",
        "col_total_raw": "总原始分",
        "col_total_scaled": "总缩放后",
        "col_current_offset": "相对基线偏移",
        "col_phase_power": "相位强度",
        "col_generates": "所生",
        "col_constrains": "所克",
        "col_phase_delta": "相位增减",
        "col_recovery_delta": "回归项",
        "col_persistent_delta": "持久项",
        "col_saturated_delta": "饱和后",
        "traits_profile": "traits / profile / 上次快照",
        "latest_snapshot": "当前提示层快照",
        "history_title": "回合列表",
        "history_empty": "还没有 history。",
        "history_item_title": "第 {index} 轮 · {target} · {context}",
        "history_tags_label": "标签",
        "history_selected": "当前选中",
        "history_turn_tags_short": "本轮标签",
        "history_body_tag_short": "身体标签",
        "recent_input": "最近输入参数",
        "body_turn": "五脏 · 本轮得分与当前分数",
        "recent_output": "最近提示层输出",
        "history_json": "完整最近一轮 history JSON",
        "selected_turn_detail": "当前选中回合详情",
        "trace_overview": "这里按处理流程展示该回合的完整数值轨迹。",
        "trace_missing": "该回合还没有 trace 数据。",
        "trace_input": "1. 输入层",
        "trace_input_summary": "输入摘要",
        "trace_context_focus": "焦点",
        "trace_base_mapping": "2. 基础映射与脆弱度缩放",
        "trace_base_mapping_table": "五脏原始分 / 缩放分汇总",
        "trace_mapping_steps": "详细映射步骤",
        "trace_stimulus_steps": "语义刺激步骤",
        "trace_reaction_steps": "身体反应步骤",
        "trace_phase": "3. 相位传播",
        "trace_phase_snapshot": "冻结快照",
        "trace_phase_actions": "源脏器决策",
        "trace_phase_result": "各目标脏器相位修正",
        "trace_persistent": "4. 持久化更新",
        "trace_persistent_table": "current 更新明细",
        "trace_output": "5. 最终提示层输出",
        "trace_output_tags": "本轮标签",
        "trace_output_body": "当前 BODY_TAG",
        "trace_output_baseline_persona": "基本人设",
        "trace_output_compare": "基线 vs 当前",
        "manual_section": "手动粘贴结构化情感分析 JSON（AnalysisInput）进行本轮试跑",
        "json_label": "JSON",
        "json_placeholder": '{"analysis_target": "...", ...}',
        "apply_turn": "应用本轮",
        "json_err": "JSON 解析失败",
        "process_err": "校验或处理失败",
        "manual_result": "手动试跑结果",
        "manual_input": "输入参数",
        "manual_output": "提示层输出",
        "turn_tags_label": "TURN_CHANGE_TAGS",
        "body_tag_label": "BODY_TAG",
        "full_mapping_json": "完整 MappingResult JSON",
    },
}


def T(lang: str, key: str) -> str:
    lg = lang if lang in STRINGS else LANG_EN
    return str(STRINGS[lg][key])
