"""知识库命令行入口"""
import sys
import json
from knowledge import rebuild_index, search, get_stats

if len(sys.argv) > 1 and sys.argv[1] == "rebuild":
    rebuild_index()
elif len(sys.argv) > 1 and sys.argv[1] == "search":
    query = " ".join(sys.argv[2:])
    results = search(query)
    for r in results:
        print(f"\n📄 {r['title']} ({r['source']}) [分数:{r.get('score', 0):.3f}]")
        print(f"   {r['text'][:200]}...")
elif len(sys.argv) > 1 and sys.argv[1] == "stats":
    print(json.dumps(get_stats(), ensure_ascii=False, indent=2))
else:
    print("用法:")
    print("  python -m knowledge rebuild      - 重建索引")
    print("  python -m knowledge search <查询>  - 搜索")
    print("  python -m knowledge stats        - 统计信息")
