"""Tests on Lab 08's recorded data (eval labs, Rubric Design). Everything is simulated: 60 support-chat summaries,
two colleagues (A and B) labeling each one pass/fail, and a team lead's overall 1-5 rating. Each string is one
labeler's labels on summaries s01..s60 in order: 1 = pass, 0 = fail.
Run: pytest test_rubric_practice.py"""
from collections import Counter

from rubric_practice import (agreement_counts, categorize, chance_agreement, cohen_kappa, labels_changed,
                             length_groups, percent_agreement, rating_gap)


def bits(s):
    return [c == "1" for c in s]


# 02_agreement.py: the team's vague rubric (v1)
PRO_A = bits("111111011111111111111111111101011111011111111101111111111111")
PRO_B = bits("100111110111101110111001001101110111111110100101111101111011")
COMPLETE_A = bits("100111111111101111010011101111110111010110000101111111001001")
COMPLETE_B = bits("011011011110001111100110110111100100000010111111011011001101")
# the rewritten rubric (v2)
TONE_A = bits("111111111111111101111101111101111111111111111111111111111111")
TONE_B = bits("101111111111111110111111111101001111111111011111111101111111")
FACTS_A = bits("111111111111011111110101111101011111110111111001101000011111")
FACTS_B = bits("111111111111011001110111111101011101110111111001101000011011")
WORDS_A = bits("111101101000010111111101111001111100101001111011000111010111")
WORDS_B = bits("111101101000010111111101111001111100101001111011000111010111")

# 03_validity.py: the lead's overall rating per summary, word counts, and which summaries are padded (truth.json)
RATINGS = [int(c) for c in "433444454444124244441454434514242355241353434214525122145424"]
WORDS = [43, 30, 22, 26, 55, 36, 45, 58, 32, 57, 55, 54, 89, 17, 99, 46, 41, 48, 26, 48, 47, 36, 59, 32, 24, 33, 37, 60, 52, 25, 40, 28, 25, 42, 67, 67, 38, 56, 32, 85, 63, 20, 38, 25, 35, 51, 17, 28, 51, 89, 54, 43, 42, 48, 54, 26, 62, 30, 40, 40]
PADDED = bits("000000000000101000000000000000000000000100000000010000000000")

# 04_drift.py: colleague A labels neutral tone on the same 20 anchor summaries in weeks 1, 3 and 6
WEEK1 = bits("11111111111111101001")
WEEK3 = bits("11111111011111100001")
WEEK6 = bits("11111111011111100000")

# 01_error_analysis.py: the reviewer's 61 notes on the first 50 summaries
NOTES = ['no order number', 'no next step for the agent', "doesn't say what happens next", 'no order number', 'good — long but everything in it is useful', 'looks fine', "can't tell which order this is about", 'good — long but everything in it is useful', 'slightly snippy about the customer?', 'fine, clear — long but everything in it is useful', 'looks fine — long but everything in it is useful', "can't tell which order this is about", 'too wordy, filler', "wrong number — doesn't match the chat", "can't tell which order this is about", "what's the action? unclear", 'rambling first half', 'refund amount is wrong', 'ok', 'slightly snippy about the customer?', 'ok', 'missing order # and product', "says 'A partial refund was already issued last week' — never said in the chat", 'forgot the exchange request', "what's the action? unclear", 'slightly snippy about the customer?', 'good', 'no next step for the agent', "doesn't say what happens next", "can't tell which order this is about", 'fine, clear — long but everything in it is useful', "says 'Customer mentioned they'd accept store credit' — never said in the chat", 'calls the customer difficult — not ok', 'good', 'blames the customer', 'refund amount is wrong', 'summary drops that they also asked for exchange', 'forgot the replacement request', "what's the action? unclear", 'only mentions one of the asks — address is missing', 'good — long but everything in it is useful', 'good — long but everything in it is useful', 'forgot the address request', "can't tell which order this is about", "where does 'Customer mentioned they'd accept store credit' come from? not in convo", 'forgot the address request', "can't tell which order this is about", 'too wordy, filler', 'fine, clear — long but everything in it is useful', 'summary drops that they also asked for address', "doesn't say what happens next", 'fine, clear', "what's the action? unclear", "doesn't say what happens next", "wrong number — doesn't match the chat", "what's the action? unclear", "wrong number — doesn't match the chat", 'looks fine', 'fine, clear — long but everything in it is useful', "says 'Customer says this is their first order with us' — never said in the chat", 'long, repeats the chat instead of summarizing']


def test_categorize_groups_the_notes():
    c = Counter(categorize(n) for n in NOTES)
    assert c == {"fine": 16, "no next step": 11, "missing order ID / amounts": 9, "missed a request": 7,
                 "judgmental tone": 5, "wrong detail": 5, "padding / repetition": 4, "invented fact": 4}
    assert sum(v for k, v in c.items() if k != "fine") == 45
    assert categorize("Looks GOOD to me") == "fine"
    assert categorize("the font is ugly") == "uncategorized"


def test_first_match_wins():
    # "never said" (invented fact) comes before "forgot" (missed a request) in CATEGORIES
    assert categorize("never said that, and forgot the address") == "invented fact"


def test_counts_professional():
    assert agreement_counts(PRO_A, PRO_B) == {"both_pass": 40, "only_a": 15, "only_b": 3, "both_fail": 2}
    try:
        agreement_counts([True], [True, False])
    except ValueError:
        pass
    else:
        raise AssertionError("different lengths must raise ValueError")


def test_professional_is_barely_above_chance():
    assert abs(percent_agreement(PRO_A, PRO_B) - 0.70) < 1e-9
    assert round(chance_agreement(PRO_A, PRO_B), 2) == 0.68   # 55/60 x 43/60 + 5/60 x 17/60
    assert round(cohen_kappa(PRO_A, PRO_B), 2) == 0.06


def test_complete_v1():
    assert round(percent_agreement(COMPLETE_A, COMPLETE_B), 2) == 0.58
    assert round(cohen_kappa(COMPLETE_A, COMPLETE_B), 2) == 0.10
    assert sum(a != b for a, b in zip(COMPLETE_A, COMPLETE_B)) == 25


def test_tone_trap():
    assert round(percent_agreement(TONE_A, TONE_B), 2) == 0.87
    assert abs(chance_agreement(TONE_A, TONE_B) - 0.845) < 1e-9
    assert round(cohen_kappa(TONE_A, TONE_B), 2) == 0.14


def test_rewritten_criterion_and_the_word_count():
    assert round(cohen_kappa(FACTS_A, FACTS_B), 2) == 0.77
    assert cohen_kappa(WORDS_A, WORDS_B) == 1.0


def test_kappa_when_everything_passes():
    assert cohen_kappa([1, 1, 1], [1, 1, 1]) == 1.0
    assert cohen_kappa([1, 1, 1], [0, 0, 0]) == 0.0


def test_validity_gaps():
    assert round(rating_gap(FACTS_A, RATINGS), 2) == 2.12
    assert round(rating_gap(WORDS_A, RATINGS), 2) == -0.63
    assert round(rating_gap(TONE_A, RATINGS), 2) == 0.02
    assert rating_gap([True, True], [4, 5]) is None


def test_two_kinds_of_long():
    g = length_groups(WORDS, RATINGS, PADDED)
    assert g["short"][0] == 39 and round(g["short"][1], 2) == 3.13
    assert g["useful"][0] == 17 and round(g["useful"][1], 2) == 4.06
    assert g["padded"] == (4, 2.5)


def test_drift_on_the_anchor_set():
    assert [sum(w) for w in (WEEK1, WEEK3, WEEK6)] == [17, 15, 14]
    assert labels_changed(WEEK1, WEEK3) == 2
    assert labels_changed(WEEK1, WEEK6) == 3
    assert round(cohen_kappa(WEEK1, WEEK3), 2) == 0.69
    assert round(cohen_kappa(WEEK1, WEEK6), 2) == 0.58
