"""
Practice: the QA agent's alarm rules from Lab 18's script 03. A QA agent runs 195 tests against
the assistant every night and decides whether to raise an alarm. Every rule is set from runs 1-3
of the unchanged assistant (`cal`); a "night" is one real run of the suite, a list of True (pass)
/ False (fail), one per test.

You'll build the paired test's reference and see why it cries wolf, then the fix: compare each
test with its own pass rate.

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


def reference(cal: list[list[bool]]) -> list[bool]:
    """The paired test's reference: what each test did in most of the calibration runs
    (passed in more than half of them)."""
    # TODO
    raise NotImplementedError


def stable(cal: list[list[bool]]) -> list[bool]:
    """True for tests that gave the same result in every calibration run (not flaky)."""
    # TODO
    raise NotImplementedError


def down_up(ref: list[bool], night: list[bool], use: list[bool] | None = None) -> tuple[int, int]:
    """(went down, went up): tests the reference calls a pass that fail tonight, and tests the
    reference calls a fail that pass tonight. `use` marks the tests to count (None = all)."""
    # TODO
    raise NotImplementedError


def mcnemar_p(down: int, up: int) -> float:
    """Exact two-sided McNemar test: only the tests that changed count, and under 'no change'
    each is a coin flip. p = 2 × P(at most min(down, up) heads in down + up flips), capped at 1.
    With no changed tests, p = 1. Hint: math.comb(n, i) / 2 ** n is P(exactly i heads)."""
    # TODO
    raise NotImplementedError


def paired_alarm(ref: list[bool], night: list[bool], use: list[bool] | None = None, alpha: float = 0.01) -> bool:
    """Alarm when more tests went down than up, and p < alpha."""
    # TODO
    raise NotImplementedError


def pass_rates(cal: list[list[bool]]) -> list[float]:
    """Each test's own pass rate, pulled toward the suite's average because three runs is few:
    (passes + k·avg) / (2k), with k calibration runs and avg the pass rate over all of them.
    A test that passed 3 of 3 gets about 0.96, not 1.0."""
    # TODO
    raise NotImplementedError


def alarm_line(rates: list[float], z: float = 3.0) -> tuple[float, float]:
    """(expected failures on a normal night, the alarm line). Expected = Σ(1 − p_i); the line is
    expected + z standard deviations, with SD = √Σ p_i(1 − p_i)."""
    # TODO
    raise NotImplementedError


def per_test_alarm(rates: list[float], night: list[bool], z: float = 3.0) -> bool:
    """Alarm when tonight's failures are past the line."""
    # TODO
    raise NotImplementedError


def slip_rate(cal: list[list[bool]], later: list[list[bool]]) -> float:
    """Among tests that passed every calibration run, the share of their results in the `later`
    runs that failed. (Regression to the mean: 'always passes' was partly luck.)"""
    # TODO
    raise NotImplementedError
