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)
template=load("clipper-template.json")
check(template["schemaVersion"]=="0.1.0", "clipper schema marker")
check("#main" in template["context"], "explicit source context")
check("白鹭" in Path("article.html").read_text(), "original article present")
def validate(v):
    if not v.get("source") or not v.get("captured") or not v.get("quote"): raise ValueError("source/date/quote required")
validate(load("reference.json")); checks.append("reference fields valid, not model output")
bad=load("reference.json");bad["source"]="";negative(validate,bad,"missing source rejected")
if args.actual:
    actual=Path(args.actual).read_text()
    check("source:" in actual and "captured:" in actual, "saved note contains provenance")
    check("无法确认" in actual, "saved note preserves source quote")
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))
