"""Remove the outcome from an assessment's text, mechanically, before any coder reads it.

Whole lines go: any line naming an impact level, a score or points; any bare number or numeral right after such a line; any block
between a heading that opens a level's requirements and the next section heading. Standard library only.
"""
import re

MARK = "[removed]"
LEVEL_WORDS = r"(?:i{1,3}|iv|[1-4]|one|two|three|four)"
OUTCOME_LINE = re.compile(
    r"impact\s*level|impact\s*category|\bniveau\b|\blevel\s*[:\-]?\s*" + LEVEL_WORDS + r"\b|\blevels?\s+of\s+impact\b"
    r"|\b(?:raw|current|mitigation|impact|total|overall|aia|final)\s+(?:impact\s+)?scores?\b|\bscores?\s*(?:of|is|was|=|:)\s*\d"
    r"|\b\d+\s*(?:pts?|points?)\b|\bpoints?\s*[:=]\s*[+-]?\s*\d"
    r"|\b(?:little\s+to\s+no|moderate|high|very\s+high)[\s-]+impact\b|\bimpact\s+is\s+(?:low|moderate|high)\b"
    r"|\bis\s+(?:a\s+)?level\b|requirements?\s+(?:specific|for\s+level)|\bappendix\s+c\b",
    re.IGNORECASE)
BLOCK_OPEN = re.compile(r"^\s*(?:section\s*\d+\s*[:.\-]?\s*)?(?:requirements?\s+specific|level\s+specific|results?\s+summary|"
                        r"impact\s+level\s+requirements?)|^\s*section\s*\d+\s*[:.\-]?\s*requirements?\s*$", re.IGNORECASE)
BLOCK_CLOSE = re.compile(r"^\s*(?:section\s*(?:\d+\b|$)|mitigation\b|project\s+details\b|about\s+the\s+system\b|impact\s+questions"
                         r"|questions\s+and\s+answers)", re.IGNORECASE)
NAME_HEADING = re.compile(r"name\s+of\s+(?:respondent|adm)\b|^\s*\d*\.?\s*job\s+title", re.IGNORECASE)
BARE = re.compile(r"^\s*(?:\d{1,3}(?:\.\d+)?\s*%?|i{1,3}|iv|\+\s*\d+)\s*$", re.IGNORECASE)
ANSWER_POINTS = re.compile(r"\(\s*[+-]?\d+\s*(?:pts?|points?)?\s*\)", re.IGNORECASE)

# Leak scan: the patterns that must have no hit in anything a coder reads.
LEAKS = [
    ("level named", re.compile(r"\blevel\s*[:\-]?\s*(?:i{1,3}|iv|[1-4])\b", re.IGNORECASE)),
    ("impact level", re.compile(r"impact\s*level", re.IGNORECASE)),
    ("overall score", re.compile(r"\b(?:raw|current|mitigation|impact|total|overall|aia|final)\s+(?:impact\s+)?scores?\b|\bscores?\s*(?:of|is|was|=|:)\s*\d", re.IGNORECASE)),
    ("points", re.compile(r"\b\d+\s*(?:pts?|points?)\b|\bpoints?\s*[:=]\s*[+-]?\s*\d", re.IGNORECASE)),
    ("impact word", re.compile(r"\b(?:little\s+to\s+no|moderate|very\s+high)\s+impact\b", re.IGNORECASE)),
    ("french level", re.compile(r"\bniveau\b", re.IGNORECASE)),
]


def redact(text):
    out, removed, in_block, after_removed, drop_name = [], 0, False, False, False
    for raw in text.splitlines():
        line = raw.rstrip()
        if drop_name and line.strip():  # the name under a respondent heading: not needed to code the system
            drop_name = False
            removed += 1
            continue
        if NAME_HEADING.search(line):
            drop_name = True
        if in_block:
            if BLOCK_CLOSE.search(line):
                in_block = False
            else:
                removed += 1
                continue
        if BLOCK_OPEN.search(line):
            in_block = True
            removed += 1
            after_removed = True
            if not out or out[-1] != MARK:
                out.append(MARK)
            continue
        if OUTCOME_LINE.search(line) or (after_removed and BARE.match(line)):
            removed += 1
            after_removed = True
            if not out or out[-1] != MARK:
                out.append(MARK)
            continue
        if line.strip():
            after_removed = False
        out.append(ANSWER_POINTS.sub("", line))
    joined = "\n".join(out) + "\n"
    for _, pattern in LEAKS:  # a phrase split across two lines of the extracted text
        joined = pattern.sub(MARK, joined)
    return joined, removed


def leaks(text):
    return {name: len(p.findall(text)) for name, p in LEAKS if p.search(text)}
