#!/usr/bin/env python3
"""Exercise only: compare ASCII percentages and {{named}} tokens in a CSV.

No network calls, packages or language-quality scoring. This is not an ICU,
HTML, currency or general locale validator. A pass still needs language review.
"""
import csv
import re
import sys
from collections import Counter
from pathlib import Path

TOKEN = re.compile(r"\{\{[A-Za-z_]\w*\}\}")
PERCENT = re.compile(r"(?<![\w.,])[0-9]+%")


def issues(source, target):
    result = []
    for label, pattern in [("named-token", TOKEN), ("ASCII-percent", PERCENT)]:
        if Counter(pattern.findall(source)) != Counter(pattern.findall(target)):
            result.append(label)
    return result


def main(path):
    flagged = 0
    with Path(path).open(encoding="utf-8", newline="") as stream:
        for row in csv.DictReader(stream):
            found = issues(row["source"], row["proposed_translation"])
            flagged += bool(found)
            print(f"{row['id']}: " + ("CHECK " + ", ".join(found) if found else "NO STRUCTURAL FLAG; language review still required"))
    print(f"{flagged} row(s) need structural investigation. No row is approved by this script.")
    return 1 if flagged else 0


if __name__ == "__main__":
    raise SystemExit(main(sys.argv[1] if len(sys.argv) > 1 else Path(__file__).with_name("exercise.csv")))
