"""
Practice: the scoring behind Lab 05, where ten free checks ran on 250 pull requests with 173
planted bugs. You'll keep only the findings a PR adds, decide when a finding "flags near" a bug,
price a gate in blocked clean PRs, and write the screenshot diff that flagged an unchanged page.

A finding is a tuple (check, rule, file, line, message).

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


def new_findings(pr: list[tuple], base: list[tuple]) -> list[tuple]:
    """The findings in `pr` that its base doesn't have: what the PR adds.

    Two findings are "the same" when check, rule, file and message match. The line is ignored,
    because a PR moves lines around. Count copies: if the base has a finding twice and the PR
    three times, one is new. Go through the PR's findings sorted by (file, line, rule); each one
    uses up a matching base finding if one is left, otherwise it's new. Return the new ones in
    that order."""
    # TODO
    raise NotImplementedError


def flags_near(defect_lines: dict[str, list[int]], buggy_new: list[tuple], clean_new: list[tuple],
               window: int = 3) -> list[tuple]:
    """The buggy PR's new findings that "flag near" the planted bug.

    defect_lines: file -> the bug's line numbers (an empty list means the whole file).
    buggy_new / clean_new: the new findings of the buggy PR and of its clean twin (the same change
    without the bug). A finding flags near the bug when the clean twin doesn't have it (use
    new_findings for that) and it's in one of the bug's files, within `window` lines of a bug line."""
    # TODO
    raise NotImplementedError


def gate_price(gates: set[str], clean_blocks: dict[str, list[str]],
               exposed_by: dict[str, list[str]], n_clean: int = 77) -> dict:
    """What a set of required checks buys and costs.

    clean_blocks: clean PR -> the checks that would turn it red (a new finding, or a failing test).
    exposed_by: buggy PR -> the checks whose catch exposes its bug.
    Return:
      exposed      buggy PRs exposed by at least one check in `gates`
      blocked      clean PRs that at least one check in `gates` turns red
      blocked_pct  blocked / n_clean, as a whole percent (round half to even is fine)"""
    # TODO
    raise NotImplementedError


def diff_ratio(a: list[list[tuple]], b: list[list[tuple]], tolerance: int = 16) -> float:
    """The share of pixels that changed between two screenshots.

    A screenshot is a list of rows; each row a list of (r, g, b) pixels, 0-255. A pixel changed
    when any channel differs by more than `tolerance`. Screenshots of different sizes count as
    fully changed (1.0): a row was added or removed. The page is flagged when this is above 0."""
    # TODO
    raise NotImplementedError
