"""
Practice: the pieces behind Lab 08's router, which sends each of 250 pull requests to auto-approve,
an AI review, a full review or two reviewers. You'll write the AUC that compares signals, the same
AUC against each PR's clean twin, the readable risk points, the tier rule, and the conversion from
"share of bugs that escape" to "escaped bugs per 100 PRs".

A PR id is "<change>-<version>": "n19-clean" is the correct PR, "n19-d1" the same change with one
planted bug. A PR's signals are a dict with keys code_lines, test_share, packages, sensitive (0/1),
blast_radius (files that import the changed code), unprotected (share of mutants that survive),
gate_flags and ai_flags (bug comments from a cheap AI pre-review).

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


def auc(scores: list[float], buggy: list[bool]) -> float:
    """The chance a random buggy PR scores higher than a random clean one.

    Compare every (buggy, clean) pair: a win counts 1, a tie 0.5, a loss 0. Return wins divided by
    the number of pairs. 0.5 is a coin flip. Raise ValueError if either group is empty."""
    # TODO
    raise NotImplementedError


def twin_auc(values: dict[str, float]) -> float:
    """AUC over (buggy, clean) pairs from the same change only: can the signal tell a PR from its twin?

    values: PR id -> the signal's value. The change is everything before the last "-"; a PR is clean
    when its version is "clean". Pair each buggy PR with the clean PR(s) of its own change, and score
    the pairs like auc() (win 1, tie 0.5). Return wins / pairs."""
    # TODO
    raise NotImplementedError


def risk_points(s: dict, shape_only: bool = False) -> int:
    """The readable score: points set by hand, before looking at any results.

      code lines // 25, at most 6
      + (packages - 1)
      + 2 if it touches a sensitive path (sensitive == 1)
      + blast_radius // 3, at most 3
      + 2 if unprotected >= 0.25         (skipped when shape_only)
      + 2 if gate_flags > 0              (skipped when shape_only)
      + 2 per AI comment, at most 4      (skipped when shape_only)
      - 1 if test_share >= 0.5

    shape_only=True keeps only what an author shapes by splitting a PR (Lab 08's 04_gaming.py)."""
    # TODO
    raise NotImplementedError


def tiers(scores: dict[str, float], rule: set[str], auto_share: float, full_share: float = 0.25) -> dict[str, int]:
    """Send every PR to a tier: 0 auto-approve, 1 AI review + quick look, 2 full review, 3 two reviewers.

    PRs in `rule` (they touch a migration, the schema or validation) go to tier 3, whatever their
    score. Sort the others by (score, PR id). The lowest round(auto_share * n) go to tier 0, the
    highest round(full_share * n) to tier 2, the rest to tier 1 (n = how many PRs are ranked)."""
    # TODO
    raise NotImplementedError


def per_100_prs(escaped_share: float, prevalence: float) -> float:
    """Escaped bugs per 100 PRs, for a team where `prevalence` of PRs have a bug.

    escaped_share: the share of bugs that no tier catches (measured on the test set).
    Round to one decimal."""
    # TODO
    raise NotImplementedError
