import re, sys
from collections import Counter
sys.path.insert(0, "/app")
from app.database import SessionLocal
from sqlalchemy import text as sa_text

QUALITATIVE = r"(?:within normal limits|normal|negative|positive|pending|trace|not detected|nonreactive|reactive|clear|absent|present|none)"
VALUE_HEAD = re.compile(rf"^\s*(?:[<>≤≥~]|\+|-)?\s*(?:\d|{QUALITATIVE})", re.I)
NAME_OK = re.compile(r"^[A-Za-z][A-Za-z0-9 ,'’\-()/%\.]{1,70}$")

def split_items(block):
    return [p for p in re.split(r"\s*[;•]\s*", block) if p.strip()]

def parse_item(seg):
    m = re.match(r"^\s*(?P<name>[^,:]{2,70}?)\s*[,:]\s*(?P<value>.+?)\s*$", seg)
    if not m: return None
    name, value = m.group("name").strip(), m.group("value").strip()
    if not NAME_OK.match(name): return None
    if not VALUE_HEAD.match(value): return None
    return name, value

db = SessionLocal()
rows = db.execute(sa_text("SELECT id, question_text FROM questions WHERE question_text IS NOT NULL")).fetchall()
semi = 0; bullets = 0; candidates = []
for qid, t in rows:
    n_semi = t.count(";"); n_bul = t.count("•")
    if n_semi >= 4 or n_bul >= 4:
        items = [parse_item(s) for s in split_items(t)]
        good = sum(1 for i in items if i)
        candidates.append((qid, n_semi, n_bul, good, len(items)))
print("stems with >=4 ';' or '•':", len(candidates))
print("of those, >=4 parseable items:", sum(1 for c in candidates if c[3] >= 4))
for c in candidates[:8]:
    print(c)
