#!/usr/bin/env python3
import os
# fix_caoxian_unicode3.py - fix stored \\uXXXX sequences
import re, sqlite3

DB_PATH = os.getenv("SEARCH_DB", "/root/search.db")

def fix_content(content):
    if not content or "\\u" not in content:
        return None
    # Replace double backslash with single, then decode \\uXXXX
    s = content.replace("\\\\", "\\")
    s = re.sub(r'\\u([0-9a-fA-F]{4})', lambda m: chr(int(m.group(1), 16)), s)
    return s if s != content else None

conn = sqlite3.connect(DB_PATH)
c = conn.cursor()

rows = c.execute("SELECT rowid, content FROM gov_raw WHERE site_name LIKE '%曹县%'").fetchall()
total = len(rows)
found = 0
fixed = 0

for rowid, content in rows:
    if content and "\\u" in content:
        found += 1
        new = fix_content(content)
        if new:
            c.execute("UPDATE gov_raw SET content = ? WHERE rowid = ?", (new, rowid))
            fixed += 1

conn.commit()
conn.close()
print(f"Total: {total}, Found with unicode: {found}, Fixed: {fixed}")
