"""
Practice: the pieces behind Lab 10's review metrics. You'll write the silent-approval check, the
revert matcher, the Wilson interval that shows how little 7 reverts can say, the "did it move?"
rule the lab uses for every metric, and a helper that finds the first week a metric notices.

A PR's history is a list of events. Each event is a tuple (login, account, action):
  account: "human", "ai" (an AI reviewer) or "bot" (any other app)
  action:  "APPROVED", "CHANGES_REQUESTED", "COMMENTED" (review submissions),
           "comment" (a comment on the PR) or "thread" (a review thread on a line)

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

import re

# "Reverts o/r#12", "Reverts #12", "revert of #12", "`git revert` of #12", "Reverts https://github.com/o/r/pull/12"
REVERT_REF = re.compile(r"\brevert(?:s|ed)?`?\s+(?:of\s+)?(?:https://github\.com/[\w.-]+/[\w.-]+/pull/|[\w.-]+/[\w.-]+#|#)(\d+)", re.I)
REVERT_WORD = re.compile(r"\brevert(?:s|ed)?\b", re.I)   # "revert", not "reverting"


def silent_approval(events: list[tuple[str, str, str]], author: str) -> bool:
    """True when a person approved and no person said anything.

    A person is a "human" account other than the PR's author. "Said anything" means a person's
    "comment", "thread", "CHANGES_REQUESTED" or "COMMENTED". AI reviewers and other bots don't
    count either way: an AI thread doesn't stop an approval being silent."""
    # TODO
    raise NotImplementedError


def revert_target(title: str, body: str, titles: dict[str, int]) -> int | None:
    """The PR number a revert PR undoes, or None if this PR isn't a revert (or the target isn't found).

    A PR is a revert when REVERT_WORD matches its title, or REVERT_REF matches its body.
    Find the target by a reference (REVERT_REF on the title, a newline, then the body: group 1 is
    the number); otherwise by a title like 'Revert "<original title>"', looked up in `titles`
    (original title -> PR number)."""
    # TODO
    raise NotImplementedError


def wilson(k: int, n: int, z: float = 1.96) -> tuple[float, float]:
    """95% Wilson score interval for k successes in n, clipped to [0, 1]. Return (0.0, 1.0) if n == 0.

        p = k / n,  d = 1 + z²/n
        centre = (p + z²/(2n)) / d
        half   = z * sqrt(p(1 - p)/n + z²/(4n²)) / d"""
    # TODO
    raise NotImplementedError


def shifted(before: list[float], after: list[float], z: float = 2.0) -> int:
    """The lab's "did it move?" rule: +1 if the after-weeks are higher, -1 if lower, 0 if within the noise.

    se = sqrt(var(before)/len(before) + var(after)/len(after)), with sample variances
    (statistics.variance). With one after-week, use var(before) for it. The change is real when
    (mean(after) - mean(before)) / se is beyond ±z. If se == 0, compare the means directly.
    Raise ValueError with fewer than two before-weeks or no after-weeks."""
    # TODO
    raise NotImplementedError


def first_notice(series: list[float], switch: int, max_after: int = 13) -> int | None:
    """The first k (1..max_after) at which shifted(series[:switch], series[switch:switch + k]) is not 0.

    None if the metric never notices within max_after weeks."""
    # TODO
    raise NotImplementedError
