"""
Practice: the measurements behind Lab 06, where the same AI reviewer saw the same planted bugs in
four presentations: the original diff, data-flow order, with a walkthrough, and split into a stack.
You'll measure the noise floor, compare two presentations bug by bug, check a stack's plan, and
size a study with people.

A run is a dict: PR id -> 1 if the reviewer found its planted bug, 0 if it missed it.

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


def noise_floor(runs: list[dict[str, int]]) -> dict:
    """How much the score moves when nothing changes.

    `runs` are several reviews of identical text, each covering the same PRs. Return:
      counts  bugs found in each run, in order
      flips   PRs found in some runs and missed in others
      spread  (max(counts) - min(counts)) as whole points of the number of PRs, rounded"""
    # TODO
    raise NotImplementedError


def paired_change(base_runs: list[dict[str, int]], new_runs: list[dict[str, int]]) -> tuple[int, int, int]:
    """The same bugs, two presentations: (base %, new %, change in points), each rounded to a whole number.

    For each bug, its find rate in one presentation is the mean over the runs that reviewed it
    (0, 0.5 or 1 with two runs). Keep the bugs reviewed in both presentations. Base % and new % are
    the means of those rates; the change is new minus base, computed before rounding."""
    # TODO
    raise NotImplementedError


def plan_problems(parts: list[dict], files: list[str]) -> list[str]:
    """Check a split plan before running CI on it. Each part is {"title": ..., "files": [...]}.

    Every file the PR changes must be in exactly one part (whole files only), and a part may not
    list a file the PR doesn't change. Return one message per problem, in this order and wording:
      "part {i} has no files"                            (i counts from 1)
      "part {i} lists {f}, which the PR doesn't change"
      "{f} is in part {j} and part {i}"                  (j: the first part that listed it)
      "not in any part: f1, f2"                          (in the order of `files`)
      "{n} parts (use 1 to 5)"
    An empty list means the plan is fine."""
    # TODO
    raise NotImplementedError


def defect_parts(defect_files: list[str], parts: list[dict]) -> list[int]:
    """The parts (numbered from 1, sorted) that hold any of the planted bug's files.
    One number means the bug is whole in one part; two or more means the split cut it in two."""
    # TODO
    raise NotImplementedError


def defects_per_arm(p1: float, p2: float, z_alpha: float = 1.96, z_power: float = 0.84) -> int:
    """Bugs per group for an unpaired study (people) to tell recall p1 from p2, at 5% two-sided
    and 80% power: (z_alpha + z_power)^2 * (p1(1-p1) + p2(1-p2)) / (p1 - p2)^2, rounded up.
    Raise ValueError if p1 == p2."""
    # TODO
    raise NotImplementedError


def break_even_seconds(cents_per_pr: float, dollars_per_hour: float) -> float:
    """How many seconds of a reviewer's time the model calls cost, at the given hourly rate."""
    # TODO
    raise NotImplementedError
