"""Compare a fixed word-matching rule with and without each Belebele passage."""

import argparse
from collections import Counter
from fractions import Fraction
from hashlib import sha256
import json
from pathlib import Path
import re
from zipfile import ZipFile

from check import ARCHIVE_SHA256, normalized


CREDIT_PER_QUESTION = 12
FILES = ("eng_Latn", "vie_Latn")
REPLACEMENT_RUNS = 16


def words(text):
    return set(re.findall(r"[^\W_]+", normalized(text).casefold()))


def credit(choices, context, correct):
    scores = [Fraction(len(choice & context), len(choice) or 1) for choice in choices]
    best = max(scores)
    if scores[correct] != best:
        return 0
    return CREDIT_PER_QUESTION // scores.count(best)


def replacement_passages(rows):
    passages = {}
    for row in rows:
        url = row["link"]
        passage = row["flores_passage"]
        if passages.setdefault(url, passage) != passage:
            raise ValueError("A source URL has multiple passages")
    # Reorder the URLs by hash so the same replacement is used across languages.
    urls = sorted(passages, key=lambda url: sha256(url.encode("utf-8")).digest())
    tokenized = {url: words(passage) for url, passage in passages.items()}
    replacements = {url: [] for url in urls}
    for offset in range(1, REPLACEMENT_RUNS + 1):
        for original, replacement in zip(urls, urls[offset:] + urls[:offset]):
            if original == replacement or passages[original] == passages[replacement]:
                raise ValueError("Replacement must come from a different page and have different text")
            replacements[original].append(tokenized[replacement])
    return replacements


def compare(rows):
    replacements = replacement_passages(rows)
    counts = Counter({key: 0 for key in (
        "questions", "without_passage_credit", "with_passage_credit",
        "higher_credit", "lower_credit", "equal_credit",
    )})
    replacement_credits = [0] * REPLACEMENT_RUNS
    for row in rows:
        choices = [words(row[f"mc_answer{index}"]) for index in range(1, 5)]
        correct = int(row["correct_answer_num"]) - 1
        question = words(row["question"])
        without = credit(choices, question, correct)
        with_passage = credit(choices, question | words(row["flores_passage"]), correct)
        counts["questions"] += 1
        counts["without_passage_credit"] += without
        counts["with_passage_credit"] += with_passage
        for index, replacement in enumerate(replacements[row["link"]]):
            replacement_credits[index] += credit(choices, question | replacement, correct)
        if with_passage > without:
            counts["higher_credit"] += 1
        elif with_passage < without:
            counts["lower_credit"] += 1
        else:
            counts["equal_credit"] += 1
    counts["with_replacement_passage_credit"] = replacement_credits[0]
    return {**counts, "replacement_credits_by_rotation": replacement_credits}


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("belebele", type=Path)
    args = parser.parse_args()
    if sha256(args.belebele.read_bytes()).hexdigest() != ARCHIVE_SHA256:
        raise ValueError("Unexpected Belebele archive hash")
    results = {}
    rosters = []
    with ZipFile(args.belebele) as archive:
        for locale in FILES:
            with archive.open(f"Belebele/{locale}.jsonl") as source:
                rows = [json.loads(line) for line in source]
            keys = {(row["link"], row["question_number"]) for row in rows}
            if len(keys) != len(rows) or len(keys) != 900:
                raise ValueError(f"Unexpected questions in {locale}")
            rosters.append(keys)
            results[locale] = compare(rows)
    if any(keys != rosters[0] for keys in rosters):
        raise ValueError("The language files have different question IDs")
    print(json.dumps({"credit_per_question": CREDIT_PER_QUESTION, "by_file": results}, indent=2, sort_keys=True))


if __name__ == "__main__":
    main()
