import sys; sys.path.insert(0,"/app")
from app.database import SessionLocal
from sqlalchemy import text as sa_text
from scripts.fix_lab_formatting import repair_units, tabulate_panels
db = SessionLocal()
rows = db.execute(sa_text("SELECT id, question_text FROM questions WHERE question_text IS NOT NULL ORDER BY id")).fetchall()
n = 0
want = set(int(x) for x in sys.argv[1:]) if len(sys.argv)>1 else None
for qid, t in rows:
    fixed, _ = repair_units(t)
    out, k = tabulate_panels(fixed)
    if not k: continue
    if want and qid not in want: continue
    n += 1
    if not want and n > 14: break
    print("="*22, qid, "="*22)
    print(out)
    print()
print("total shown:", n)
