"""Check the four choices in each row of the original Belebele archive."""

import argparse
from collections import Counter
from hashlib import sha256
import json
from pathlib import Path
import unicodedata
from zipfile import ZipFile


ARCHIVE_SHA256 = "c645e750e111404806751509b5aa3f808a01e6029cd225d37e966dc694aca9e4"


def normalized(text):
    return " ".join(unicodedata.normalize("NFC", text).split())


def check_row(row, locale, counts):
    if row["dialect"] != locale:
        raise ValueError(f"Unexpected row dialect in {locale}")
    options = [row[f"mc_answer{n}"] for n in range(1, 5)]
    marked = int(row["correct_answer_num"]) - 1
    if not 0 <= marked < len(options) or not all(x.strip() for x in options):
        raise ValueError(f"Bad answer label or empty choice in {locale}")
    counts["questions"] += 1
    for method, choices in (("exact", options), ("unicode_and_whitespace", list(map(normalized, options)))):
        matches = [i for i, option in enumerate(choices) if option == choices[marked]]
        counts[f"{method}_any_repeated_choice"] += len(set(choices)) != 4
        counts[f"{method}_marked_choice_repeats"] += len(matches) > 1
        counts[f"{method}_repeat_only_in_other_choices"] += len(set(choices)) != 4 and len(matches) == 1
        counts[f"{method}_first_matching_index_loses_credit"] += matches[0] != marked
        counts[f"{method}_last_matching_index_loses_credit"] += matches[-1] != marked
    return options.count(options[marked]) > 1


def analyze(path):
    if sha256(path.read_bytes()).hexdigest() != ARCHIVE_SHA256:
        raise ValueError("Unexpected Belebele archive digest")
    totals = Counter()
    by_locale = {}
    roster = {}
    repeated_marked_choices = Counter()
    with ZipFile(path) as archive:
        files = sorted(name for name in archive.namelist() if name.endswith(".jsonl"))
        for name in files:
            locale = Path(name).stem
            if locale in by_locale:
                raise ValueError(f"Duplicate locale file for {locale}")
            counts = Counter()
            question_keys = []
            with archive.open(name) as source:
                for line in source:
                    row = json.loads(line)
                    key = row["link"], row["question_number"]
                    question_keys.append(key)
                    if check_row(row, locale, counts):
                        repeated_marked_choices[key] += 1
            if len(set(question_keys)) != len(question_keys):
                raise ValueError(f"Question key is not unique in {locale}")
            roster[locale] = question_keys
            if counts["questions"] != 900:
                raise ValueError(f"Unexpected question count for {locale}")
            by_locale[locale] = counts
            totals.update(counts)
    english_keys = roster["eng_Latn"]
    if any(set(keys) != set(english_keys) for keys in roster.values()):
        raise ValueError("The language files do not contain the same question keys")
    return {
        "total": totals,
        "shared_question_keys": len(english_keys),
        "files_in_a_different_question_order_from_english": sum(keys != english_keys for keys in roster.values()),
        "question_keys_with_an_exact_repeated_marked_choice": len(repeated_marked_choices),
        "question_keys_with_an_exact_repeated_marked_choice_in_every_file": sum(
            n == len(roster) for n in repeated_marked_choices.values()
        ),
        "files": len(by_locale),
        "files_with_an_exact_repeated_marked_choice": sum(
            row["exact_marked_choice_repeats"] > 0 for row in by_locale.values()
        ),
        "files_with_a_repeated_marked_choice_after_cleanup": sum(
            row["unicode_and_whitespace_marked_choice_repeats"] > 0 for row in by_locale.values()
        ),
        "locales": by_locale,
    }


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("archive", type=Path)
    args = parser.parse_args()
    print(json.dumps(analyze(args.archive), indent=2, sort_keys=True))


if __name__ == "__main__":
    main()
