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):
    raw=Path(path).read_bytes()
    if not raw or b"<html" in raw.lower() or b"<!doctype" in raw.lower():raise ValueError("HTML is not CSV")
    data=rows(path)
    if len(data)!=6 or set(data[0])!={"date","station","minutes"}:raise ValueError("wrong report structure")
    if min(r["date"] for r in data)!="2026-10-01" or max(r["date"] for r in data)!="2026-10-06" or sum(int(r["minutes"]) for r in data)!=200:raise ValueError("report facts differ")
validate("report.csv");checks.append("real CSV content/date/total verified")
negative(validate,"negative.html","HTML 200-style error page rejected")
if args.actual:validate(args.actual);checks.append("actual downloaded file validated, Agent behavior requires separate record")
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))
