#!/usr/bin/env python3
"""Fix pdfId collisions, remove junk 90xxx questions, rebuild questions.json."""
from __future__ import annotations

import json
import subprocess
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
SCRIPTS = Path(__file__).resolve().parent
if str(SCRIPTS) not in sys.path:
    sys.path.insert(0, str(SCRIPTS))

REVIEW_PATH = ROOT / "data" / "questions-review.json"
QUESTIONS_PATH = ROOT / "data" / "questions.json"


def main() -> int:
    from document_ids import assign_document_ids, is_junk_id

    review = json.loads(REVIEW_PATH.read_text(encoding="utf-8"))
    changes = assign_document_ids(review)
    REVIEW_PATH.write_text(
        json.dumps(review, ensure_ascii=False, indent=2),
        encoding="utf-8",
    )
    print(f"Updated {REVIEW_PATH.name}: {len(changes)} pdfId changes")

    # Rebuild questions.json (preserves translations via reviewSeq)
    rc = subprocess.call([sys.executable, str(SCRIPTS / "apply_review.py")])
    if rc != 0:
        return rc

    questions = json.loads(QUESTIONS_PATH.read_text(encoding="utf-8"))
    before = len(questions)
    kept = [q for q in questions if not is_junk_id(int(q["id"]))]
    removed = before - len(kept)
    if removed:
        QUESTIONS_PATH.write_text(
            json.dumps(kept, ensure_ascii=False, indent=2),
            encoding="utf-8",
        )
        print(f"Removed {removed} junk questions (id >= 90003). Kept {len(kept)}.")

    # Verify case study 407 / 458
    for qid in (407, 408, 458):
        hits = [q for q in kept if q["id"] == qid]
        if hits:
            stem = (hits[0].get("stem") or {}).get("en") or ""
            print(f"  id {qid}: {stem[:70]}...")
        else:
            print(f"  id {qid}: MISSING")

    junk_left = sum(1 for q in kept if is_junk_id(int(q["id"])))
    print(f"Junk ids remaining: {junk_left}")
    return 0


if __name__ == "__main__":
    sys.exit(main())
