#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""verify_yingquan_fts.py —— 第 9 步 FTS 验证（两条路都测）"""
import json
import re
import sqlite3
import urllib.parse
import urllib.request

BASE = "http://127.0.0.1:8000"
SITE = "颍泉区人民政府-公告公示"
WORDS = ["大盘香", "氨基葡萄糖盐酸盐", "颍泉区公开引进"]
CK = "/tmp/ck_yingquan.txt"

db = sqlite3.connect("/root/search.db", timeout=20)
db.execute("PRAGMA busy_timeout=20000")

print("=== ① DB 层 gov_search MATCH（site_name 写进 SQL，不先 LIMIT 再过滤）===")
for w in WORDS:
    try:
        rows = list(db.execute(
            "SELECT g.rowid, g.title FROM gov_search g JOIN gov_raw r ON r.id=g.rowid "
            "WHERE gov_search MATCH ? AND r.site_name=? LIMIT 3", (w, SITE)))
        print("  MATCH %-12s → %d 命中 %s" % (w, len(rows), [r[1][:24] for r in rows]))
    except Exception as e:
        print("  MATCH %-12s → ❌ %s" % (w, str(e)[:60]))

n = db.execute("SELECT COUNT(*) FROM gov_bigram WHERE rowid IN (SELECT id FROM gov_raw WHERE site_name=?)",
               (SITE,)).fetchone()[0]
print("\n=== ④ gov_bigram 预计算索引 ===")
print("  本站行数:", n, "（0 = 尚未重建，次日 04:25 增量重建，属预期）")

print("\n=== ③ 登录 + HTTP ===")
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor())
try:
    r = opener.open(urllib.request.Request(
        BASE + "/login", data=b"username=admin",
        headers={"Content-Type": "application/x-www-form-urlencoded"}), timeout=20)
    print("  POST /login → HTTP", r.getcode())
except Exception as e:
    print("  ❌ 登录:", str(e)[:70])

w = WORDS[0]
try:
    h = opener.open(BASE + "/crawler/?q=" + urllib.parse.quote(w), timeout=40).read().decode("utf-8", "ignore")
    m = re.search(r"window\.__BOOT__ = (\{.*?\});</script>", h, re.S)
    if m:
        boot = json.loads(m.group(1))
        items = boot.get("items") or []
        mine = [it for it in items if "颍泉" in (it.get("site") or "")]
        print("  /crawler/?q=%s → 首屏 %d 条，本站 %d 条" % (w, len(items), len(mine)))
        for it in items[:4]:
            print("     ·", (it.get("site") or "?")[:20], "|", (it.get("title") or "")[:32])
    else:
        print("  ⚠️ 未拿到 BOOT")
except Exception as e:
    print("  ❌ /crawler/:", str(e)[:70])

print("\n=== ③ 详情页渲染 ===")
row = db.execute("SELECT id, title FROM gov_raw WHERE site_name=? ORDER BY publish_date DESC LIMIT 1", (SITE,)).fetchone()
if row:
    rid, rt = row
    try:
        h = opener.open(BASE + "/crawler/project/%s" % rid, timeout=30).read().decode("utf-8", "ignore")
        print("  id=%s HTTP 200 字节=%d" % (rid, len(h)))
        print("  含标题:", rt[:18] in h, "| 含 <p>:", "<p" in h, "| 含附件链接:", "/download?" in h)
        print("  含噪声(扫一扫/顶部/打印):", any(x in h for x in ["扫一扫", "j-goTop", "qrcode"]))
    except Exception as e:
        print("  ❌ 详情:", str(e)[:70])

print("\n=== ③ 附件可下载性实测（完整 Chrome UA + Referer）===")
arow = db.execute("SELECT attachments FROM gov_raw WHERE site_name=? AND attachments IS NOT NULL AND attachments!='' LIMIT 1",
                  (SITE,)).fetchone()
if arow:
    att = json.loads(arow[0])[0]
    req = urllib.request.Request(att["url"], headers={
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
                      "(KHTML, like Gecko) Chrome/120.0 Safari/537.36",
        "Referer": BASE + "/", "Accept": "*/*"})
    try:
        r = urllib.request.urlopen(req, timeout=40)
        data = r.read()
        print("  %s" % att["title"][:44])
        print("  URL:", att["url"])
        print("  HTTP %s | %d 字节 | Content-Type: %s" % (r.getcode(), len(data), r.headers.get("Content-Type")))
        print("  是真实文件:", "✅" if len(data) > 1000 else "⚠️ 偏小")
    except Exception as e:
        print("  ❌ 下载失败:", str(e)[:90])
db.close()
