"""将 export/session_summaries/out/ 下的单条摘要 .txt 合并为 _summaries.json。

每个 .txt 文件名为 {short_id}.txt，内容为纯文本摘要。
输出: export/session_summaries/_summaries.json
格式: {"short_id": "摘要文本", ...}

用法:
  python -B scripts/ops/merge_session_summaries.py
"""

import json
from pathlib import Path

from _env_paths import ensure_repo_root

ensure_repo_root()

OUT_DIR = Path("export/session_summaries/out")
DST = Path("export/session_summaries/_summaries.json")


def main():
    if not OUT_DIR.exists():
        print(f"目录不存在: {OUT_DIR}")
        return

    txt_files = sorted(OUT_DIR.glob("*.txt"))
    print(f"扫描到 {len(txt_files)} 个摘要文件")

    result: dict[str, str] = {}
    skipped = 0
    for f in txt_files:
        short_id = f.stem
        content = f.read_text(encoding="utf-8").strip()
        if not content:
            skipped += 1
            continue
        result[short_id] = content

    DST.write_text(
        json.dumps(result, indent=2, ensure_ascii=False) + "\n",
        encoding="utf-8",
    )
    print(f"已写入 {DST}: {len(result)} 条摘要, 跳过 {skipped} 条空文件")


if __name__ == "__main__":
    main()
