"""
Practice: the arithmetic behind the capstone's pick. Seven review setups ran over the same 250
pull requests (173 with one planted bug, 38 of them high severity, the "serious" ones); every
review and check was recorded by earlier chapters, and every "person" is an AI stand-in. You'll
write how one PR's bug gets caught, a person's minutes, the conversion to bugs through per 100 PRs,
the rule that picks a setup, and what a pick means for a team over a quarter.

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


def caught(exposed: bool, person_finds: bool, ai_findings: int, keep: float = 1.0, act: float = 1.0) -> float:
    """The chance this PR's planted bug is fixed before merge (an expected value, not a coin flip).

    - A free check that exposes the bug (exposed), or a person who finds it themselves
      (person_finds), catches it for sure: return 1.0.
    - Otherwise each of the ai_findings AI comments that describe the bug survives the finding check
      with chance `keep` (1.0 when there's no check). The bug is caught if at least one survives
      AND someone acts on it, with chance `act` (1.0 when a person reads the PR; the author's rate
      when nobody does).
    - With no AI finding on the bug, return 0.0."""
    # TODO
    raise NotImplementedError


def person_minutes(changed_lines: int, tier: str, wrong_comments: float = 0.0, lines_per_hour: float = 500,
                   min_full: float = 5, quick_look: float = 3, per_wrong: float = 2) -> float:
    """A person's minutes on one PR: reading time by tier, plus per_wrong minutes per wrong comment read.

    A full read is changed_lines / lines_per_hour hours, in minutes, but at least min_full.
    Tiers: "auto" (nobody reads) 0, "quick" quick_look, "full" a full read, "two" twice a full read."""
    # TODO
    raise NotImplementedError


def per_100(escaped_bugs: float, buggy: int = 173, prevalence: float = 0.10) -> float:
    """Bugs through per 100 PRs of real traffic.

    The test set has a bug in 69% of its PRs; a team has far fewer. So take the share of buggy PRs
    whose bug escapes (escaped_bugs / buggy) and scale it to `prevalence`: 100 x prevalence x share.
    Don't round."""
    # TODO
    raise NotImplementedError


def choose(setups: dict[str, dict], max_per_100: float = 2.0, serious_ref: str | None = "humans") -> str | None:
    """The capstone's pick. Each setup is {"minutes": ..., "per_100": ..., "serious": ...}.

    A setup passes when per_100 <= max_per_100 and, if serious_ref is set, its serious count is
    no higher than setups[serious_ref]["serious"] (compare with a 1e-9 tolerance). Return the name
    of the passing setup with the fewest minutes, or None if none passes."""
    # TODO
    raise NotImplementedError


def per_quarter(per_100_prs: float, prs_per_week: int = 60, weeks: int = 13) -> float:
    """Bugs through in a quarter for a team merging prs_per_week PRs (13 weeks). Don't round."""
    # TODO
    raise NotImplementedError
