from pathlib import Path
import argparse
import copy
import csv
import hashlib
import io
import json
import re

parser = argparse.ArgumentParser()
parser.add_argument("--actual")
parser.add_argument("--local-http", action="store_true")
args = parser.parse_args()
checks = []
def check(condition, label):
    if not condition:
        raise ValueError(label)
    checks.append(label)
def load(name): return json.loads(Path(name).read_text())
def rows(name): return list(csv.DictReader(io.StringIO(Path(name).read_text(encoding="utf-8-sig"))))
def negative(fn, value, label):
    try: fn(value)
    except (ValueError, AssertionError, KeyError, TypeError, IndexError): checks.append(label)
    else: raise AssertionError("Negative input accepted: " + label)
def validate(path):
    data=rows(path)
    if len(data)!=8: raise ValueError("eight rows required")
    for r in data:
        if len(re.findall(r"\{\{c1::[^{}]+\}\}",r["Text"]))!=1 or not r["Extra"] or r["Tags"]!="quickrouter-cloze":raise ValueError("Cloze/source/tag invalid")
validate("cards.csv");checks.append("eight Cloze notes, one c1 and source per note")
negative(validate,"negative.csv","missing Cloze braces rejected")
check(len(rows("glossary.csv"))==8,"eight original glossary terms")
if args.actual: validate(args.actual);checks.append("actual CSV validated; Anki UI import not inferred")
report={"layer":"offline/material checks, not proof of unrun product or model", "checks":checks, "count":len(checks)}
Path("verification.json").write_text(json.dumps(report,ensure_ascii=False,indent=2))
print(json.dumps(report,ensure_ascii=False))
