#!/usr/bin/env python3
"""Offline lint for EADA-20260831. Stdlib only. No network. No product client."""
from __future__ import annotations

import csv
import json
import pathlib
import sys

ROOT = pathlib.Path(__file__).resolve().parent
DISCLAIMER = "STATIC FIXTURE / NOT CONNECTED / NOT INDEPENDENTLY VALIDATED. DO NOT EXECUTE."


def fail(msg: str) -> None:
    print(f"FAIL: {msg}", file=sys.stderr)
    raise SystemExit(1)


def read_csv(name: str) -> list[dict[str, str]]:
    path = ROOT / name
    if not path.is_file():
        fail(f"missing {name}")
    with path.open(newline="", encoding="utf-8") as fh:
        return list(csv.DictReader(fh))


def main() -> None:
    print(DISCLAIMER)
    identity = read_csv("identity-register-EADA-20260831.csv")
    held = read_csv("held-evidence-fields-EADA-20260831.csv")
    decisions = read_csv("identity-decision-register-EADA-20260831.csv")
    assumptions = read_csv("assumption-register-EADA-20260831.csv")

    for row in identity + held:
        if row.get("value") != "HELD" or row.get("status") != "HELD":
            fail(f"identity/held must stay HELD: {row}")

    by_id = {row["id"]: row for row in decisions}
    if by_id["EADA-Q1-IDENTITY"]["outcome"] != "HOLD / NOT READY":
        fail("Q1 must HOLD")
    if by_id["EADA-Q2-HOST-FIELDS"]["outcome"] != "QUALIFIED FOR STATIC REVIEW":
        fail("Q2 must be QUALIFIED FOR STATIC REVIEW")
    if by_id["EADA-Q3-SYNC-TIMEOUT"]["outcome"] != "REJECTED AS UNSUPPORTED":
        fail("Q3 must be REJECTED")
    if by_id["EADA-Q4-BROWSER-KEY"]["outcome"] != "REJECTED AS UNSUPPORTED":
        fail("Q4 must be REJECTED")
    if any(row.get("selected", "").lower() == "yes" for row in decisions):
        fail("no row may be selected=yes")

    note = (ROOT / "accepted-host-note-EADA-20260831.md").read_text(encoding="utf-8")
    for required in ("tenant", "requester", "task id", "status enum"):
        if required not in note:
            fail(f"host note must name {required}")

    expected = json.loads((ROOT / "expected-readiness-EADA-20260831.json").read_text(encoding="utf-8"))
    if expected.get("overall_outcome") != "HOLD / NOT READY FOR CONNECTION":
        fail("expected overall outcome mismatch")

    if len(assumptions) < 7:
        fail("assumption register incomplete")

    protocol = (ROOT / "independent-reproduction-protocol-EADA-20260831.md").read_text(encoding="utf-8")
    if "no network" not in protocol.lower():
        fail("protocol must forbid network")

    print("PASS: file agreement only. HOLD / NOT READY FOR CONNECTION")
    print("Do not invent a task id latency, a weekly reuse, or an opened SQL match.")


if __name__ == "__main__":
    main()
