#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""数据层修复转换器 v2：修复 v1 遗漏的 :--- 格式 md 表格 + 兜底全量扫描
v1 的 SELECT 用 LIKE '%| ---%' 只匹配无冒号格式，漏掉了 | :--- | 记录。
v2 直接全表游标扫描，md_table_to_html 已支持 :--- 分隔行（^:?-{2,}:?$）。
"""
import sqlite3, re, sys

MD_LINK_RE = re.compile(r'\[([^\]]{1,200})\]\((https?://[^)\s]+)\)')
MD_IMG_RE = re.compile(r'!\[([^\]]*)\]\((https?://[^)\s]+)\)')


def md_table_to_html(text):
    """把正文中 md 管道表格块转为 HTML <table>（支持 :--- 分隔行）"""
    lines = text.split('\n')
    out = []
    i = 0
    n = len(lines)
    while i < n:
        line = lines[i]
        m = re.match(r'^\s*\|(.+)\|\s*$', line)
        if m and '|' in m.group(1):
            rows = []
            j = i
            while j < n:
                mm = re.match(r'^\s*\|(.+)\|\s*$', lines[j])
                if mm and '|' in mm.group(1):
                    rows.append(lines[j])
                    j += 1
                else:
                    break
            if len(rows) >= 2:
                parsed = []
                for r in rows:
                    cells = [c.strip() for c in r.strip().strip('|').split('|')]
                    if all(re.match(r'^:?-{2,}:?$', c) for c in cells if c):
                        continue  # 分隔行（含 :---）
                    parsed.append(cells)
                if parsed:
                    trs = []
                    for cells in parsed:
                        tds = ''.join(f'<td>{md_inline_to_html(c)}</td>' for c in cells)
                        trs.append(f'<tr>{tds}</tr>')
                    tbl = '<table><tbody>' + ''.join(trs) + '</tbody></table>'
                    out.append(tbl)
                    i = j
                    continue
        out.append(line)
        i += 1
    return '\n'.join(out)


def md_inline_to_html(cell):
    cell = MD_IMG_RE.sub(lambda m: f'<img src="{m.group(2)}" alt="{m.group(1)}">', cell)
    cell = MD_LINK_RE.sub(lambda m: f'<a href="{m.group(2)}">{m.group(1)}</a>', cell)
    cell = re.sub(r'\*\*([^*]+)\*\*', r'<strong>\1</strong>', cell)
    return cell


def content_to_html(content):
    content = md_table_to_html(content)
    content = MD_IMG_RE.sub(lambda m: f'<img src="{m.group(2)}" alt="{m.group(1)}">', content)
    lines = content.split('\n')
    new_lines = []
    for line in lines:
        m = re.match(r'^\s*\[([^\]]{1,200})\]\((https?://[^)\s]+)\)\s*$', line)
        if m:
            new_lines.append(f'<p><a href="{m.group(2)}">{m.group(1)}</a></p>')
        else:
            new_lines.append(MD_LINK_RE.sub(lambda m2: f'<a href="{m2.group(2)}">{m2.group(1)}</a>', line))
    return '\n'.join(new_lines)


def main():
    dry = '--dry' in sys.argv
    conn = sqlite3.connect("/root/search.db", timeout=60)
    conn.execute("PRAGMA busy_timeout=60000")
    conn.execute("PRAGMA journal_mode=WAL")
    c = conn.cursor()

    # 全表游标扫描（fetchmany 迭代，不用 OFFSET——雪花 rowid 不连续且 OFFSET 大表超慢）
    c.execute("SELECT COUNT(*) FROM gov_raw")
    total = c.fetchone()[0]
    print(f"总行数: {total}", flush=True)

    updated = 0
    skipped = 0
    errors = 0
    conn.execute("BEGIN")
    c.execute("SELECT rowid, site_name, content, summary FROM gov_raw")
    while True:
        rows = c.fetchmany(10000)
        if not rows:
            break
        for rowid, site, content, summary in rows:
            if content is None:
                skipped += 1
                continue
            # 预筛：不含 md 特征直接跳过（避免每行跑完整正则链）
            if not ('|' in content or '](http' in content or '![http' in content or ':---' in content):
                skipped += 1
                continue
            try:
                new_content = content_to_html(content)
                new_summary = summary
                if summary and '](http' in summary:
                    new_summary = MD_LINK_RE.sub(
                        lambda m: f'<a href="{m.group(2)}">{m.group(1)}</a>', summary)
                if new_content != content or new_summary != summary:
                    if not dry:
                        c.execute("UPDATE gov_raw SET content=?, summary=? WHERE rowid=?", (new_content, new_summary, rowid))
                    updated += 1
                else:
                    skipped += 1
            except Exception as e:
                errors += 1
                print(f"  ❌ rowid {rowid}: {e}", flush=True)
            if updated % 5000 == 0 and updated > 0:
                if not dry:
                    conn.commit()
                    conn.execute("BEGIN")
                print(f"  已更新 {updated} ...", flush=True)
        if not dry:
            conn.commit()
            conn.execute("BEGIN")

    if not dry:
        conn.commit()
    conn.close()
    print(f"\n=== 完成 ===")
    print(f"更新: {updated} / 跳过: {skipped} / 错误: {errors} (dry={dry})")


if __name__ == '__main__':
    main()
