"""
Practice: classify AI use cases by EU AI Act risk level the way Lab 17's script 02 does. A model
answers factual questions about a use case; code applies the Act's rules in order. Then compare
two methods on the same cases, and set the company's own risk tier. Not legal advice.

Fill in the TODOs, then run the tests:
    pytest test_risk_practice.py        (locally)
or press "Run tests" on the chapter page.
"""
import math

PROHIBITED = {"a": "subliminal or manipulative techniques", "b": "exploiting vulnerabilities", "c": "social scoring",
              "d": "crime prediction from profiling alone", "e": "untargeted facial image scraping",
              "f": "emotion recognition at work or school", "g": "biometric categorisation by sensitive traits",
              "h": "real-time remote biometric ID for law enforcement",
              "ba": "sexual images of a real person without consent", "bb": "child sexual abuse material"}
AREAS = ["biometrics", "critical_infrastructure", "education", "employment", "essential_services",
         "law_enforcement", "migration", "justice_democracy", "none"]
EXCEPTIONS = ["none", "narrow_procedural_task", "improves_completed_human_activity",
              "detects_patterns_without_replacing_human_review", "preparatory_task"]
ORDER = {"prohibited": 3, "high": 2, "limited": 1, "minimal": 0}


def decide(a: dict) -> tuple[str, str]:
    """The Act's order, strictest first. Return (level, basis):
    1. a banned practice (a key of PROHIBITED)       -> ("prohibited", "Art. 5(1)(<key>) <name>")
    2. a high-risk area (not "none"):
         no valid exception, or it profiles people   -> ("high", "Annex III, <area>")
         else, with an exception                     -> basis "Art. 6(3) <exception>, Annex III <area>",
            "limited" (basis + "; Art. 50") if it talks with people or makes content, else "minimal"
    3. talks with people, makes content, or reads emotions / biometric traits -> ("limited", "Art. 50")
    4. otherwise                                     -> ("minimal", "none")"""
    # TODO
    raise NotImplementedError


def score(levels: list[str], labels: list[str]) -> dict:
    """right: levels equal to the label. too_low: levels below the label (the costly mistake)."""
    # TODO
    raise NotImplementedError


def mcnemar(a_right: list[bool], b_right: list[bool]) -> dict:
    """Paired comparison on the same cases. Only the cases where exactly one method was right count;
    under "no difference" each is a coin flip. Return {"b_only", "a_only", "p"}: p is the exact
    two-sided binomial tail, 2 x P(X <= min(a_only, b_only)) for X ~ Binomial(a_only + b_only, 1/2), capped at 1."""
    # TODO
    raise NotImplementedError


def internal_tier(severity: int, likelihood: int, reversible: bool, affects_people: bool) -> str:
    """severity and likelihood 1-4. Score = severity x likelihood, +4 if it can't be undone,
    +2 if it harms specific people. 16+ critical, 10+ high, 5+ medium, else low."""
    # TODO
    raise NotImplementedError
