import re, sys; sys.path.insert(0,"/app")
from collections import Counter
from app.database import SessionLocal
from sqlalchemy import text as sa_text
db = SessionLocal()
rows = db.execute(sa_text("""SELECT id, question_text, explanation FROM questions
                             WHERE image_path IS NOT NULL AND image_path <> ''""")).fetchall()
Q = re.compile(r"\b[Il]tem\s*Q\s*\d+", re.I)
C = re.compile(r"\b[Il]tem\s*C\s*\d+", re.I)
b = Counter()
for qid, s, e in rows:
    b[(bool(Q.search(s or "")), bool(C.search(s or "")), bool(C.search(e or "")), bool(Q.search(e or "")))] += 1
print("(stemQ, stemC, explC, explQ) -> n")
for k,v in sorted(b.items(), key=lambda kv:-kv[1]): print("  ", k, v)
# how many overall mention any Item label
print("\nany Item-Q in stem:", sum(1 for _,s,_ in rows if Q.search(s or "")))
print("any Item-C in expl:", sum(1 for _,_,e in rows if C.search(e or "")))
