"""
Practice: does the prompt style help? Lab 15's math (style.py, 01_plan.py, 05_effects.py): Flesch reading
ease, a keyword coverage rule, the per-conversation average of 3 runs, the 2×2 main effects and interaction,
a shuffled-label noise floor, and the decision rule written before the run.

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

Rates are fractions (0.97, not 97%). Arms are always in the order normal, STE, point-and-call, both.
Standard library only.
"""
import random
import re

ARMS = ["normal", "ste", "pc", "both"]


# ---------------------------------------------------------------------------------------------------------
# Given: the lab's sentence and word splitters (style.py), so your Flesch matches the lab's to the decimal.

def sentences(text):
    """Sentences, with each line of a list counted as its own sentence."""
    out = []
    for line in text.splitlines():
        line = re.sub(r"^\s*(?:[-*•]|\d+[.)])\s*", "", line).strip()
        out += [s.strip() for s in re.split(r"(?<=[.!?;])\s+", line) if re.search(r"[A-Za-z0-9]", s)]
    return out


def words(text):
    return re.findall(r"[A-Za-z0-9$#'’–-]+", text)


# ---------------------------------------------------------------------------------------------------------

def syllables(word):
    """The lab's rough syllable count:
    1. lowercase the word and keep only the letters a-z; if nothing is left (a number, a symbol), return 1
    2. count groups of vowels (runs of a, e, i, o, u, y)
    3. if the word ends in 'e' but not in 'le', 'ee' or 'ye', and there is more than one group, subtract 1
    4. subtract 1 for each silent e before a suffix: a non-vowel, then 'e', then ment / ly / ful / ness
    5. return at least 1"""
    # TODO
    raise NotImplementedError


def flesch(text):
    """Flesch reading ease: 206.835 - 1.015 * (words per sentence) - 84.6 * (syllables per word).
    Higher is easier. Use sentences(), words() and syllables()."""
    # TODO
    raise NotImplementedError


def fact_present(text, tokens):
    """Lab 09's coverage rule for one key fact: every token must appear in the text (case-insensitive,
    as a substring). A token may list alternatives separated by '|': "tracking|delivery" is found if
    either word is there."""
    # TODO
    raise NotImplementedError


def per_conversation(runs):
    """runs: {arm: [[run1, run2, run3], ...]} one list of 3 run scores per conversation.
    Return {arm: [mean of the 3 runs, ...]}: one number per conversation, so each conversation counts once."""
    # TODO
    raise NotImplementedError


def contrasts(normal, ste, pc, both):
    """Per-conversation contrasts of a 2×2 experiment (each argument: one value per conversation).
    STE            = ((ste - normal) + (both - pc)) / 2
    point-and-call = ((pc - normal) + (both - ste)) / 2
    interaction    = (both - pc) - (ste - normal)
    Return {"STE": [...], "point-and-call": [...], "interaction": [...]}."""
    # TODO
    raise NotImplementedError


def effects(per_arm):
    """The three effects: the mean of each contrast over conversations. per_arm as per_conversation returns."""
    # TODO
    raise NotImplementedError


def null_band(rows, n_perm=1000, seed=0):
    """A noise floor for the STE effect. rows: one list of 12 scores per conversation (normal x3, ste x3,
    pc x3, both x3). n_perm times: shuffle each conversation's 12 scores (random.Random(seed).shuffle on a
    copy), split them back into 4 arms of 3, average each arm, and compute the STE effect (the mean over
    conversations of ((ste - normal) + (both - pc)) / 2). Return (low, high): the 2.5th and 97.5th
    percentiles of those n_perm effects (sort them; take the values at positions int(0.025 * n_perm)
    and int(0.975 * n_perm) - 1)."""
    # TODO
    raise NotImplementedError


def verdict(style_effects, plan, harm_share=0.0):
    """The decision rule from 01_plan.py, for one style.
    style_effects: {outcome: (estimate, low, high)}; plan: {outcome: (bar, better)} where better is +1 when
    higher is better and -1 when lower is better.
    An outcome HELPS if better * estimate >= bar and the interval excludes 0 (low > 0 or high < 0).
    It HURTS if it got worse with an interval that excludes 0 (better = +1: high < 0; better = -1: low > 0)
    and -better * estimate >= harm_share * bar (harm_share = 0: any size counts, as the lab's rule).
    Return "adopt" (something helps, nothing hurts), "reject" (something hurts, nothing helps),
    "trade-off" (both) or "no clear effect" (neither)."""
    # TODO
    raise NotImplementedError
