"""Per-exam paths and PDF extraction settings."""
from __future__ import annotations

import re
from dataclasses import dataclass
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


@dataclass(frozen=True)
class ExamProfile:
    slug: str
    code: str
    pdf_name: str
    expected_questions: int
    certyiq_base: str
    data_dir: Path
    assets_dir: Path
    preface_seq_max: int
    content_start_patterns: tuple[str, ...]

    @property
    def pdf_path(self) -> Path:
        return ROOT / self.pdf_name

    @property
    def questions_json(self) -> Path:
        return self.data_dir / "questions.json"

    @property
    def questions_review_json(self) -> Path:
        return self.data_dir / "questions-review.json"

    @property
    def manifest_json(self) -> Path:
        return self.data_dir / "manifest.json"

    @property
    def review_manifest_json(self) -> Path:
        return self.data_dir / "questions-review-manifest.json"

    @property
    def translate_checkpoint(self) -> Path:
        return self.data_dir / ".translate_checkpoint.json"


EXAMS: dict[str, ExamProfile] = {
    "az-104": ExamProfile(
        slug="az-104",
        code="AZ-104",
        pdf_name="AZ-104 (634Qs).pdf",
        expected_questions=634,
        certyiq_base="https://certyiq.com/papers/microsoft/az-104",
        data_dir=ROOT / "data",
        assets_dir=ROOT / "assets" / "questions",
        preface_seq_max=2,
        content_start_patterns=(
            r"Your company has serval departments",
            r"Your company has several departments",
        ),
    ),
    "az-305": ExamProfile(
        slug="az-305",
        code="AZ-305",
        pdf_name="AZ-305 (288Qs).pdf",
        expected_questions=288,
        certyiq_base="https://certyiq.com/papers/microsoft/az-305",
        data_dir=ROOT / "data" / "az-305",
        assets_dir=ROOT / "assets" / "az-305" / "questions",
        preface_seq_max=0,
        content_start_patterns=(
            r"You need to recommend",
            r"You need to ",
            r"You have an Azure",
            r"HOTSPOT\s*-",
        ),
    ),
    "az-400": ExamProfile(
        slug="az-400",
        code="AZ-400",
        pdf_name="docs/az-400.pdf",
        expected_questions=624,
        certyiq_base="https://certyiq.com/papers/microsoft/az-400",
        data_dir=ROOT / "data" / "az-400",
        assets_dir=ROOT / "assets" / "az-400" / "questions",
        preface_seq_max=0,
        content_start_patterns=(
            r"You are configuring project metrics",
            r"624\s+Questions",
            r"Select [`']No adjustment required",
            r"You are making use of Azure",
            r"You are developing",
            r"You have an Azure",
            r"Your company",
            r"HOTSPOT\s*-",
            r"DRAG DROP\s*-",
            r"You need to ",
        ),
    ),
}


def get_profile(slug: str) -> ExamProfile:
    key = slug.lower().strip()
    if key not in EXAMS:
        raise ValueError(f"Unknown exam {slug!r}; choose from: {', '.join(EXAMS)}")
    return EXAMS[key]


def normalize_pdf_text(text: str, profile: ExamProfile) -> str:
    text = text.replace("\t", " ")
    text = re.sub(
        r"Question:\s*(\d+)\s*(?:Certy\s*IQ\s*)+",
        r"<<<Q:\1>>>",
        text,
        flags=re.I,
    )
    text = re.sub(r"Certy\s*IQ", "", text, flags=re.I)
    for pat in profile.content_start_patterns:
        start = re.search(pat, text, re.I)
        if start:
            text = text[start.start() :]
            break
    return text


def parse_cli_exam(argv: list[str] | None = None) -> ExamProfile:
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--exam",
        default="az-104",
        choices=sorted(EXAMS.keys()),
        help="Exam slug (default: az-104)",
    )
    args, _ = parser.parse_known_args(argv)
    return get_profile(args.exam)
