"""Solution for pipeline_practice.py (the capstone: the review pipeline)."""


def caught(exposed: bool, person_finds: bool, ai_findings: int, keep: float = 1.0, act: float = 1.0) -> float:
    if exposed or person_finds:
        return 1.0
    if ai_findings <= 0:
        return 0.0
    delivered = 1 - (1 - keep) ** ai_findings
    return delivered * act


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:
    full = max(min_full, changed_lines / lines_per_hour * 60)
    reading = {"auto": 0.0, "quick": quick_look, "full": full, "two": 2 * full}[tier]
    return reading + wrong_comments * per_wrong


def per_100(escaped_bugs: float, buggy: int = 173, prevalence: float = 0.10) -> float:
    return 100 * prevalence * escaped_bugs / buggy


def choose(setups: dict[str, dict], max_per_100: float = 2.0, serious_ref: str | None = "humans") -> str | None:
    cap = setups[serious_ref]["serious"] if serious_ref else float("inf")
    ok = [name for name, s in setups.items()
          if s["per_100"] <= max_per_100 + 1e-9 and s["serious"] <= cap + 1e-9]
    return min(ok, key=lambda n: setups[n]["minutes"]) if ok else None


def per_quarter(per_100_prs: float, prs_per_week: int = 60, weeks: int = 13) -> float:
    return per_100_prs / 100 * prs_per_week * weeks
