#!/usr/bin/env python3
"""Daily backfill: classify industry for new rows that slipped through without industry."""
import sqlite3, sys, os

DB = "/root/search.db"
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))

# Load INDUSTRY_KEYWORDS from crawler_lib
sys.path.insert(0, "/root/gov_crawler")
from crawler_lib import classify_industry

db = sqlite3.connect(DB)
db.execute("PRAGMA journal_mode=WAL")
db.execute("PRAGMA synchronous=OFF")

processed = 0
hits = 0
batch = db.execute(
    "SELECT id, title FROM gov_raw "
    "WHERE (title LIKE '%项目%' OR title LIKE '%工程%') "
    "AND (industry IS NULL OR industry = '' OR industry = 'other') "
    "LIMIT 1000"
).fetchall()

if not batch:
    print("No unclassified rows found.")
    db.close()
    exit(0)

updates = []
others = []
for rid, title in batch:
    ind = classify_industry(title or "")
    if ind:
        updates.append((ind, rid))
        hits += 1
    else:
        others.append((rid,))

if updates:
    db.executemany("UPDATE gov_raw SET industry = ? WHERE id = ?", updates)
if others:
    db.executemany("UPDATE gov_raw SET industry = 'other' WHERE id = ?", others)
db.commit()

processed = len(batch)
print(f"Processed {processed} rows, {hits} classified.")
db.close()
