"""One-off: migrate the WOTD tables (gloss/usage) and backfill LLM polish for every
pooled word that lacks it, then refresh today's pick from the now-polished pool.
Run on the host with the LLM env sourced. Idempotent — safe to re-run."""
import json
import os

from goodnews import wotd
from goodnews.db import connect, init_db
from goodnews.llm import LocalModelClient

conn = connect(os.environ.get("GOODNEWS_DB", "data/goodnews.sqlite3"))
init_db(conn)                       # idempotent migration: adds gloss/usage columns
client = LocalModelClient.from_env()

rows = conn.execute(
    "SELECT id, word, part_of_speech, definition FROM wotd_pool WHERE gloss IS NULL OR gloss=''"
).fetchall()
print(f"pool words to polish: {len(rows)}")
for r in rows:
    p = wotd._polish(client, r["word"], r["part_of_speech"], r["definition"])
    if p:
        conn.execute("UPDATE wotd_pool SET gloss=?, usage=? WHERE id=?",
                     (p["gloss"], json.dumps(p["examples"]), r["id"]))
        print(f"  ✓ {r['word']}: {p['gloss']}")
    else:
        print(f"  – {r['word']}: polish unavailable, raw kept")
conn.commit()

picked = wotd.pick_daily(conn, force=True, client=client)   # today, from the polished pool
print(f"\ntoday → {picked.get('word')}")
print(f"  gloss: {picked.get('gloss')}")
print(f"  usage: {picked.get('usage')}")
conn.close()
