"""
Solution: score an AI code reviewer like a classifier (Lab 03). Same functions as harness_practice.py.
"""
import random


def recall_per_run(found: list[list[int]]) -> list[int]:
    """For each run, how many bugs it found."""
    if not found:
        return []
    return [sum(row[r] for row in found) for r in range(len(found[0]))]


def pooled(found: list[list[int]]) -> int:
    """How many bugs at least one run found."""
    return sum(1 for row in found if any(row))


def only_by(a: list[list[int]], b: list[list[int]]) -> tuple[int, int]:
    """(found only by a, found only by b), over all runs."""
    only_a = sum(1 for x, y in zip(a, b) if any(x) and not any(y))
    only_b = sum(1 for x, y in zip(a, b) if any(y) and not any(x))
    return only_a, only_b


def paired_bootstrap(a: list[list[int]], b: list[list[int]], iterations: int = 2000,
                     seed: int = 0) -> tuple[float, float, float]:
    """b minus a, mean of runs per bug, with a paired 95% bootstrap interval."""
    rng = random.Random(seed)
    diffs = [sum(y) / len(y) - sum(x) / len(x) for x, y in zip(a, b)]
    n = len(diffs)
    point = sum(diffs) / n
    boots = sorted(sum(diffs[rng.randrange(n)] for _ in range(n)) / n for _ in range(iterations))
    return point, boots[int(0.025 * iterations)], boots[int(0.975 * iterations) - 1]


def precision(right: int, unmatched: int) -> float:
    """Share of bug comments that were right."""
    total = right + unmatched
    return right / total if total else 0.0


def proven(on_pr: str, with_fix: str) -> bool:
    """Fails on the PR, passes with the fix."""
    return on_pr == "fails" and with_fix == "passes"
