"""
Practice: rubric design. Lab 08's agreement and validity math (evalkit/agreement.py and Scripts 1-4):
group review notes into failure categories, measure how often two labelers agree, how much of that is
chance, Cohen's kappa, whether passing a criterion goes with better overall ratings, and whether one
labeler's own labels drift over time.

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

Labels are lists of True/False (or 1/0), one per summary, in the same order for both labelers
(True = pass). Standard library only.
"""

# Script 1's keyword rules for grouping notes ("axial coding"). Order matters: the first match wins.
CATEGORIES = [
    ("invented fact", ["never said", "made up", "come from", "not in convo"]),
    ("wrong detail", ["amount is wrong", "wrong number", "doesn't match"]),
    ("missing order ID / amounts", ["order number", "which order", "order #"]),
    ("missed a request", ["forgot", "drops", "missing"]),
    ("no next step", ["what happens next", "next step", "action"]),
    ("judgmental tone", ["difficult", "rude", "blames", "snippy", "judgy", "tone"]),
    ("padding / repetition", ["repeats", "rambling", "wordy"]),
    ("fine", ["fine", "good", "ok", "clear"]),
]


def categorize(note):
    """Return the name of the first category in CATEGORIES with a keyword contained in the note
    (case-insensitive), or "uncategorized" if none matches."""
    # TODO
    raise NotImplementedError


def agreement_counts(a, b):
    """Return {"both_pass": .., "only_a": .., "only_b": .., "both_fail": ..}: how many items both passed,
    only A passed, only B passed, both failed. Raise ValueError if a and b have different lengths."""
    # TODO
    raise NotImplementedError


def percent_agreement(a, b):
    """Share of items where A and B gave the same label."""
    # TODO
    raise NotImplementedError


def chance_agreement(a, b):
    """Agreement you'd expect if each labeler kept their own pass rate but labeled at random:
    pA * pB + (1 - pA) * (1 - pB)."""
    # TODO
    raise NotImplementedError


def cohen_kappa(a, b):
    """(observed agreement - chance agreement) / (1 - chance agreement).
    If chance agreement is 1 (both gave one label to everything), kappa is undefined: return 1.0 if
    they also agree on every item, else 0.0 (the lab's rule)."""
    # TODO
    raise NotImplementedError


def rating_gap(passed, ratings):
    """Validity check: the mean overall rating of items that pass minus the mean of items that fail.
    passed: list of True/False; ratings: list of numbers, same order. Return None if nothing passes or
    nothing fails."""
    # TODO
    raise NotImplementedError


def length_groups(words, ratings, padded, limit=50):
    """Mean rating of three groups: "short" (words <= limit), "useful" (over the limit, not padded) and
    "padded" (over the limit and padded). words, ratings, padded: lists in the same order (padded is
    True/False). Return {"short": (count, mean), "useful": (count, mean), "padded": (count, mean)}."""
    # TODO
    raise NotImplementedError


def labels_changed(before, after):
    """How many items got a different label the second time (a drift check on an anchor set)."""
    # TODO
    raise NotImplementedError
