#!/usr/bin/env python3
"""一次性脚本：为所有缺少 description 的主对话 entry 打上占位标记。

这样 batch_generate_summaries.py 不会每次从头重新处理。
用户之后可以手动在终端跑 batch_generate_summaries.py 覆盖这些占位值。

用法：
  python -B scripts/ops/_patch_missing_descriptions.py              # 执行
  python -B scripts/ops/_patch_missing_descriptions.py --dry-run    # 预览
"""

import json
import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__)))
from extract_kiro_session import load_index, save_index, load_full_index, save_full_index

PLACEHOLDER = "[待生成摘要]"


def main():
    dry_run = "--dry-run" in sys.argv

    index = load_index()
    full_index = load_full_index()

    patched = 0
    idx_entries = index.get("entries", {})
    full_entries = full_index.get("entries", {})

    for eid, ent in idx_entries.items():
        if ent.get("is_sub"):
            continue
        if not ent.get("description"):
            if not dry_run:
                ent["description"] = PLACEHOLDER
                if eid in full_entries:
                    full_entries[eid]["description"] = PLACEHOLDER
            patched += 1

    if dry_run:
        print(f"预览：将为 {patched} 条 entry 打上占位标记 '{PLACEHOLDER}'")
        return

    if patched > 0:
        save_index(index)
        save_full_index(full_index)

    print(f"完成：已为 {patched} 条 entry 打上占位标记 '{PLACEHOLDER}'")
    print("后续可在终端手动运行 batch_generate_summaries.py 覆盖生成真实摘要")


if __name__ == "__main__":
    main()
