#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""觅寻验证 API 最小示例（真实调用本机 App）

前置：启动觅寻桌面版（http://127.0.0.1:17420）
运行：python examples/validate_python.py "发票 对账"
"""
import json
import sys
import urllib.request

BASE = "http://127.0.0.1:17420"


def main() -> int:
    keyword = sys.argv[1] if len(sys.argv) > 1 else "发票 对账"
    body = json.dumps({"keyword": keyword, "limit": 12}, ensure_ascii=False).encode()
    req = urllib.request.Request(BASE + "/v1/validate", data=body,
                                 headers={"Content-Type": "application/json"}, method="POST")
    with urllib.request.urlopen(req, timeout=300) as r:
        data = json.loads(r.read().decode("utf-8", "ignore"))

    cov = data.get("coverage") or {}
    print(data.get("auto_summary", ""))
    print(f"\n结论：{(data.get('conclusion') or {}).get('verdict')} · 评分 {data.get('total')}/100")
    print(f"证据强度：{cov.get('level')}（来源 {cov.get('signals')} 条 / 平台 {cov.get('platforms')} 个）\n来源：")
    for i, s in enumerate((data.get("signals") or [])[:10], 1):
        kind = {"complaint": "抱怨", "ask": "求助", "doc": "资料"}.get(s.get("kind"), "")
        print(f"{i:>2}. [{kind}] {s.get('title')[:52]}\n    {s.get('url')}")
    return 0


if __name__ == "__main__":
    sys.exit(main())
