#!/usr/bin/env python3
"""Reference verifier for an AgWaterAI DAG-TAT attestation chain.

Companion to DAG-TAT-CANONICALIZATION-v1.md. This file implements the
specification and nothing else: it imports no AgWaterAI code and needs only
the Python standard library plus the `cryptography` package
(`pip install cryptography`). Two optional packages enable the optional
sub-checks of step 8: `opentimestamps-client` (OpenTimestamps proofs) and
`rfc3161ng` (RFC 3161 tokens). Without them the proof is reported as present
but not checked locally, never as verified.

Two modes:

  1. Public mode: the authenticated envelope HTTPS endpoints published at
     https://agwaterai.com/api/v1/verify/{farm}. Proves signatures, linkage,
     contiguity, tenant isolation, head agreement and the time anchor. It
     cannot recompute payload_hash because the sealed content is not served.

       python3 verify_dag_tat.py --public tenant-a
       python3 verify_dag_tat.py --public tenant-a --from 19700

     Set AGWATER_VERIFY_TOKEN to a farm-authorized Bearer credential.
     The API allows 120 requests per 15 minutes per client address.
     One block is one request, so use --from for long chains.

  2. Artifact mode: a directory holding the bucket objects (blocks/*.json,
     genesis.json, head.json, keys/v1.pem and optionally anchors/). Proves
     everything public mode proves AND recomputes payload_hash, every DAG
     node hash, every diamond, every leaf hash and every Merkle root from
     the sealed content.

       python3 verify_dag_tat.py --dir ./artifacts --tenant tenant-a

Exit status 0 when every check passed, 1 otherwise. Counts are printed so a
report can quote them.
"""
from __future__ import annotations

import argparse
import base64
import glob
import hashlib
import json
import os
import sys
import urllib.request
import urllib.error
from collections import Counter

from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.asymmetric import utils as asym_utils

SPEC = "DAG-TAT-CANONICALIZATION-v1"
PUBLIC_BASE = "https://agwaterai.com/api/v1/verify"
GENESIS_PREV_HASH = "0" * 64

# Section 4.1 of the spec: exactly these nine keys enter the payload hash.
PAYLOAD_FIELDS = (
    "domain_separator", "block_number", "event_type", "event_data",
    "sealed_at", "prev_hash", "tenant_id", "key_epoch", "key_version",
)
# Section 4.3: the head snapshot that a time anchor digests.
HEAD_FIELDS = ("block_number", "payload_hash", "sealed_at", "event_type", "tenant_id", "schema")


# --------- Section 2 and 3: canonical form and hash --------------------------

def canonical_json(obj) -> str:
    return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)


def sha256_hex(text: str) -> str:
    return hashlib.sha256(text.encode("utf-8")).hexdigest()


def b64url_decode(s: str) -> bytes:
    return base64.urlsafe_b64decode(s + "=" * (-len(s) % 4))


# --------- Section 5: signature -----------------------------------------------

def signature_valid(pub, block: dict) -> bool:
    """ECDSA P-256 over the raw 32-byte payload_hash digest (prehashed), DER
    encoded signature, base64url without padding."""
    try:
        pub.verify(
            b64url_decode(block["signature"]),
            bytes.fromhex(block["payload_hash"]),
            ec.ECDSA(asym_utils.Prehashed(hashes.SHA256())),
        )
        return True
    except (InvalidSignature, ValueError, KeyError):
        return False


# --------- Section 6: sealed content ------------------------------------------

def merkle_root(leaf_hashes_sorted: list[str]) -> str:
    """Pairwise SHA-256 over the concatenated lowercase HEX STRINGS, last
    element duplicated when a level has odd length. Empty input hashes ""."""
    if not leaf_hashes_sorted:
        return sha256_hex("")
    level = list(leaf_hashes_sorted)
    while len(level) > 1:
        nxt = []
        for i in range(0, len(level), 2):
            a = level[i]
            b = level[i + 1] if i + 1 < len(level) else level[i]
            nxt.append(sha256_hex(a + b))
        level = nxt
    return level[0]


def node_hash(node: dict) -> str:
    """Section 6.2: a DAG node hash is SHA-256 of the canonical form of
    {datapoint_id, node_type, parent_token_hashes (sorted), body, sealed_at}."""
    parts = {
        "datapoint_id": node["datapoint_id"],
        "node_type": node["node_type"],
        "parent_token_hashes": sorted(node["parent_token_hashes"]),
        "body": node["body"],
        "sealed_at": node["sealed_at"],
    }
    return sha256_hex(canonical_json(parts))


def check_event_content(ed: dict, counts: Counter, fails: list[str], n: int) -> None:
    """Recompute everything inside event_data that the spec makes recomputable."""
    leaves = ed.get("leaves")
    if leaves is None:
        counts["content_without_leaves"] += 1   # genesis and reconciliation blocks
        return
    lh = [lf["leaf_hash"] for lf in leaves]
    if lh != sorted(lh):
        counts["leaf_order_FAIL"] += 1
        fails.append(f"block {n}: leaves are not sorted by leaf_hash")
    else:
        counts["leaf_order_ok"] += 1
    if ed.get("leaf_count") == len(lh):
        counts["leaf_count_ok"] += 1
    else:
        counts["leaf_count_FAIL"] += 1
        fails.append(f"block {n}: leaf_count {ed.get('leaf_count')} != {len(lh)}")
    if merkle_root(sorted(lh)) == ed.get("merkle_root"):
        counts["merkle_root_ok"] += 1
    else:
        counts["merkle_root_FAIL"] += 1
        fails.append(f"block {n}: merkle_root does not recompute")

    nodes = ed.get("dag_nodes") or []
    by_hash = {}
    for node in nodes:
        if "sealed_at" not in node:
            # Section 6.2: nodes sealed before 2026-07-27 omit sealed_at, which
            # is part of the preimage, so their hashes are committed but not
            # recomputable from the stored node. Counted, never passed.
            counts["dag_node_hash_not_recomputable_no_sealed_at"] += 1
        elif node_hash(node) == node["hash"]:
            counts["dag_node_hash_ok"] += 1
        else:
            counts["dag_node_hash_FAIL"] += 1
            fails.append(f"block {n}: node hash mismatch {node.get('node_type')} {node.get('datapoint_id')}")
        by_hash[node["hash"]] = node
    if ed.get("dag_node_count") is not None:
        counts["dag_node_count_ok" if ed["dag_node_count"] == len(nodes) else "dag_node_count_FAIL"] += 1

    diamonds = ed.get("dag_diamonds") or []
    attestation_hashes = set()
    for d in diamonds:
        ok = (
            d["left_path"][0] == d["ancestor_hash"] == d["right_path"][0]
            and d["left_path"][1] != d["right_path"][1]
            and d["convergence_hash"] in by_hash
            and sorted(by_hash[d["convergence_hash"]]["parent_token_hashes"])
            == sorted([d["left_path"][1], d["right_path"][1]])
            and by_hash[d["left_path"][1]]["parent_token_hashes"] == [d["ancestor_hash"]]
            and by_hash[d["right_path"][1]]["parent_token_hashes"] == [d["ancestor_hash"]]
        )
        counts["diamond_ok" if ok else "diamond_FAIL"] += 1
        if not ok:
            fails.append(f"block {n}: diamond {d.get('convergence_hash', '')[:16]} is not a converged two-path diamond")
        attestation_hashes.add(d["convergence_hash"])
    # Every leaf_hash is the convergence (attestation) node of one diamond.
    if set(lh) == attestation_hashes:
        counts["leaves_are_diamond_attestations_ok"] += 1
    else:
        counts["leaves_are_diamond_attestations_FAIL"] += 1
        fails.append(f"block {n}: leaf_hash set differs from the diamond convergence set")


# --------- Section 7: time anchors ---------------------------------------------

def check_anchor(record: dict, payload_hash_of_block, counts: Counter, fails: list[str],
                 bitcoin_api: str | None) -> None:
    n = record.get("anchored_block_number")
    head = record.get("anchored_head") or {}
    digest = sha256_hex(canonical_json(head))
    if digest == record.get("anchored_bytes_sha256"):
        counts["anchor_digest_ok"] += 1
    else:
        counts["anchor_digest_FAIL"] += 1
        fails.append(f"anchor {n}: anchored_bytes_sha256 != sha256(canonical(anchored_head))")
    if head.get("payload_hash") == record.get("anchored_payload_hash") and \
            int(head.get("block_number", -1)) == int(n):
        counts["anchor_head_fields_ok"] += 1
    else:
        counts["anchor_head_fields_FAIL"] += 1
        fails.append(f"anchor {n}: anchored_head disagrees with the record's own fields")
    walked = payload_hash_of_block(int(n))
    if walked is None:
        counts["anchor_block_not_in_walk"] += 1
    elif walked == record.get("anchored_payload_hash"):
        counts["anchor_matches_walked_block_ok"] += 1
    else:
        counts["anchor_matches_walked_block_FAIL"] += 1
        fails.append(f"anchor {n}: anchored payload_hash differs from block {n} in the walk")

    method = record.get("anchor_method")
    try:
        _check_anchor_proof(record, method, n, counts, fails, bitcoin_api)
    except (KeyError, ValueError, TypeError) as e:
        counts["anchor_proof_malformed"] += 1
        fails.append(f"anchor {n}: proof record is malformed")


def _check_anchor_proof(record: dict, method, n, counts: Counter, fails: list[str],
                        bitcoin_api: str | None) -> None:
    if method == "opentimestamps":
        try:
            from opentimestamps.core.notary import BitcoinBlockHeaderAttestation  # type: ignore
            from opentimestamps.core.serialize import BytesDeserializationContext  # type: ignore
            from opentimestamps.core.timestamp import DetachedTimestampFile  # type: ignore
        except ImportError:
            counts["anchor_proof_present_not_checked_locally"] += 1
            return
        raw = base64.b64decode(record["ots_proof_b64"])
        detached = DetachedTimestampFile.deserialize(BytesDeserializationContext(raw))
        if detached.file_digest != bytes.fromhex(record["anchored_bytes_sha256"]):
            counts["anchor_proof_FAIL"] += 1
            fails.append(f"anchor {n}: OTS proof digest != anchored_bytes_sha256")
            return
        atts = list(detached.timestamp.all_attestations())
        bitcoin = [(m, a) for m, a in atts if isinstance(a, BitcoinBlockHeaderAttestation)]
        if not bitcoin:
            counts["anchor_proof_pending_at_calendar"] += 1
            return
        if not bitcoin_api:
            counts["anchor_proof_bitcoin_not_checked_no_header_source"] += 1
            return
        for msg, att in bitcoin:
            try:
                with urllib.request.urlopen(f"{bitcoin_api.rstrip('/')}/block-height/{int(att.height)}", timeout=20) as r:
                    block_hash = r.read().decode().strip()
                with urllib.request.urlopen(f"{bitcoin_api.rstrip('/')}/block/{block_hash}", timeout=20) as r:
                    root = json.loads(r.read().decode())["merkle_root"]
            except Exception as e:  # noqa: BLE001
                counts["anchor_proof_bitcoin_fetch_error"] += 1
                fails.append(f"anchor {n}: could not fetch Bitcoin block {att.height}: {type(e).__name__}")
                continue
            if msg[::-1].hex() == root.lower():
                counts["anchor_proof_bitcoin_ok"] += 1
            else:
                counts["anchor_proof_bitcoin_FAIL"] += 1
                fails.append(f"anchor {n}: Bitcoin block {att.height} merkle root does not match the proof")
    elif method == "rfc3161":
        try:
            import rfc3161ng  # type: ignore
            from cryptography import x509
        except ImportError:
            counts["anchor_proof_present_not_checked_locally"] += 1
            return
        cert_der = x509.load_pem_x509_certificate(record["tsa_certificate_pem"].encode()).public_bytes(
            serialization.Encoding.DER)
        tst = base64.b64decode(record["tst_der_b64"])
        try:
            rfc3161ng.check_timestamp(tst, certificate=cert_der,
                                      digest=bytes.fromhex(record["anchored_bytes_sha256"]),
                                      hashname="sha256", nonce=int(record["nonce"]))
            counts["anchor_proof_rfc3161_ok"] += 1
        except Exception as e:  # noqa: BLE001
            counts["anchor_proof_rfc3161_FAIL"] += 1
            fails.append(f"anchor {n}: RFC 3161 token did not verify: {type(e).__name__}")
    else:
        counts["anchor_method_unknown"] += 1
        fails.append(f"anchor {n}: unknown anchor method")


# --------- envelope checks shared by both modes ---------------------------------

def check_envelope(blocks: list[dict], keys: dict[str, object], head: dict | None,
                   counts: Counter, fails: list[str], expect_complete_from_zero: bool,
                   allow_gaps: bool = False) -> None:
    by_n = {int(b["block_number"]): b for b in blocks}
    nums = sorted(by_n)
    tenants = Counter(b["tenant_id"] for b in blocks)
    counts["blocks_checked"] = len(blocks)
    for n in nums:
        b = by_n[n]
        key_id = b["key_version"]
        pub = keys.get(key_id) or keys.get(f"v{b['key_epoch']}")
        if pub is None:
            counts["signature_no_key_for_epoch"] += 1
            fails.append(f"block {n}: public key unavailable")
        elif signature_valid(pub, b):
            counts["signature_ok"] += 1
        else:
            counts["signature_FAIL"] += 1
            fails.append(f"block {n}: signature invalid")
        if b.get("signature_alg") != "EC_SIGN_P256_SHA256" or b.get("schema") != "dag-tat-v1":
            counts["envelope_labels_FAIL"] += 1
            fails.append(f"block {n}: unexpected signature_alg/schema labels")
        if n == 0:
            counts["genesis_prev_ok" if b["prev_hash"] == GENESIS_PREV_HASH else "genesis_prev_FAIL"] += 1
        elif n - 1 in by_n:
            if b["prev_hash"] == by_n[n - 1]["payload_hash"]:
                counts["linkage_ok"] += 1
            else:
                counts["linkage_FAIL"] += 1
                fails.append(f"block {n}: prev_hash != payload_hash of block {n-1}")
        else:
            counts["linkage_unchecked_no_predecessor"] += 1
    gaps = [(nums[i], nums[i + 1]) for i in range(len(nums) - 1) if nums[i + 1] != nums[i] + 1]
    counts["numbering_gaps"] = len(gaps)
    if gaps and allow_gaps:
        # A partial artifact set (genesis plus a tail, say). Linkage across the
        # hole is unchecked and counted above; the hole itself is not a finding.
        print(f"  note: numbering gaps in this artifact set (allowed): {gaps[:10]}")
    elif gaps:
        fails.append(f"block numbering gaps: {gaps[:10]}")
    if expect_complete_from_zero and nums and nums[0] != 0:
        fails.append("walk does not start at genesis (block 0)")
    counts["distinct_tenant_ids"] = len(tenants)
    if len(tenants) != 1:
        fails.append("more than one tenant identity in the chain")
    if head is not None and nums:
        top = by_n[nums[-1]]
        if int(head["block_number"]) == nums[-1]:
            ok = head["payload_hash"] == top["payload_hash"] and head["sealed_at"] == top["sealed_at"] \
                and head["event_type"] == top["event_type"] and head["tenant_id"] == top["tenant_id"]
            counts["head_matches_top_block" if ok else "head_MISMATCH"] += 1
            if not ok:
                fails.append("head.json disagrees with the highest block walked")
        else:
            counts["head_beyond_walk"] += 1


# --------- artifact mode ---------------------------------------------------------

def load_keys_from_dir(d: str) -> dict[str, object]:
    keys: dict[str, object] = {}
    for pem in glob.glob(os.path.join(d, "keys", "v*.pem")):
        epoch = os.path.basename(pem)[:-4]
        keys[epoch] = serialization.load_pem_public_key(open(pem, "rb").read())
    return keys


def run_dir(d: str, tenant: str, bitcoin_api: str | None, allow_gaps: bool = False) -> int:
    counts: Counter = Counter()
    fails: list[str] = []
    blocks = []
    for f in sorted(glob.glob(os.path.join(d, "blocks", "*.json"))):
        with open(f) as fh:
            blocks.append(json.load(fh))
    blocks.sort(key=lambda b: int(b["block_number"]))
    if not blocks:
        print("no blocks found")
        return 1
    keys = load_keys_from_dir(d)
    # key_version is a KMS resource name ending in /cryptoKeyVersions/<n>; map to v<n>.
    for b in blocks:
        kv = b["key_version"].rsplit("/", 1)[-1]
        if f"v{kv}" in keys:
            keys[b["key_version"]] = keys[f"v{kv}"]
    head = None
    hp = os.path.join(d, "head.json")
    if os.path.exists(hp):
        head = json.load(open(hp))
        if canonical_json({k: head.get(k) for k in HEAD_FIELDS}) != canonical_json(head):
            fails.append("head.json carries fields outside the specified head layout")
    for b in blocks:
        n = int(b["block_number"])
        if b["tenant_id"] != tenant:
            counts["tenant_FAIL"] += 1
            fails.append(f"block {n}: tenant identity mismatch")
        payload = {k: b[k] for k in PAYLOAD_FIELDS}
        if sha256_hex(canonical_json(payload)) == b["payload_hash"]:
            counts["payload_hash_ok"] += 1
        else:
            counts["payload_hash_FAIL"] += 1
            fails.append(f"block {n}: payload_hash does not recompute")
        check_event_content(b.get("event_data") or {}, counts, fails, n)
    check_envelope(blocks, keys, head, counts, fails, expect_complete_from_zero=False,
                   allow_gaps=allow_gaps)
    gp = os.path.join(d, "genesis.json")
    if os.path.exists(gp) and int(blocks[0]["block_number"]) == 0:
        g = json.load(open(gp))
        counts["genesis_mirror_ok" if canonical_json(g) == canonical_json(blocks[0]) else "genesis_mirror_FAIL"] += 1
    by_n = {int(b["block_number"]): b["payload_hash"] for b in blocks}
    for f in sorted(glob.glob(os.path.join(d, "anchors", "*", "*.json"))):
        check_anchor(json.load(open(f)), by_n.get, counts, fails, bitcoin_api)
        counts["anchors_checked"] += 1
    return report(f"artifact mode  blocks {blocks[0]['block_number']}..{blocks[-1]['block_number']}",
                  counts, fails)


# --------- public mode ------------------------------------------------------------

class NoCredentialRedirect(urllib.request.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, headers, newurl):
        raise ValueError('Authenticated verification does not follow redirects')


def _get(url: str, raw: bool = False):
    token = os.environ.get("AGWATER_VERIFY_TOKEN", "")
    if not token:
        raise ValueError("AGWATER_VERIFY_TOKEN is required for remote verification")
    if not url.startswith("https://"):
        raise ValueError("Authenticated verification requires HTTPS")
    req = urllib.request.Request(url, headers={
        "User-Agent": f"verify_dag_tat/{SPEC}", "Authorization": f"Bearer {token}",
    })
    with urllib.request.build_opener(NoCredentialRedirect).open(req, timeout=45) as r:
        body = r.read()
    return body if raw else json.loads(body)


def run_public(farm: str, base: str, first: int | None, last: int | None, bitcoin_api: str | None) -> int:
    counts: Counter = Counter()
    fails: list[str] = []
    man = _get(f"{base}/{farm}")
    if man.get("signature_algorithm") != "EC_SIGN_P256_SHA256" or man.get("hash_algorithm") != "SHA-256":
        fails.append("manifest names an algorithm this verifier does not implement")
    head = _get(f"{base}/{farm}/head")
    top = int(head["block_number"])
    first = 0 if first is None else first
    last = top if last is None else min(last, top)
    keys: dict[str, object] = {}
    blocks = []
    for n in range(first, last + 1):
        b = _get(f"{base}/{farm}/block/{n}")
        kv = b["key_version"].rsplit("/", 1)[-1]
        if b["key_version"] not in keys:
            pem = _get(f"{base}/{farm}/keys/v{kv}", raw=True)
            keys[b["key_version"]] = serialization.load_pem_public_key(pem)
            counts["public_keys_fetched"] += 1
        if "event_data" in b:
            fails.append(f"block {n}: public endpoint served event_data (should be redacted)")
        blocks.append(b)
    check_envelope(blocks, keys, head, counts, fails, expect_complete_from_zero=(first == 0))
    counts["payload_hash_recomputable_publicly"] = 0
    by_n = {int(b["block_number"]): b["payload_hash"] for b in blocks}

    listing = _get(f"{base}/{farm}/anchors")
    counts["anchors_listed"] = int(listing.get("count", 0))
    if listing.get("count"):
        newest = listing["anchors"][-1]
        rec = _get(f"{base}/{farm}/anchor/{newest['block_number']}")

        def walked(n: int):
            if n in by_n:
                return by_n[n]
            try:
                return _get(f"{base}/{farm}/block/{n}")["payload_hash"]
            except Exception:  # noqa: BLE001
                return None
        check_anchor(rec, walked, counts, fails, bitcoin_api)
        counts["anchors_checked"] += 1
    else:
        counts["step8_time_anchor_available"] = 0
    return report(f"authenticated envelope mode  blocks {first}..{last} of head {top}", counts, fails)


def report(title: str, counts: Counter, fails: list[str]) -> int:
    print(f"### {SPEC}  {title}")
    for k in sorted(counts):
        print(f"  {k}: {counts[k]}")
    print("### FAILURES" if fails else "### FAILURES: none")
    for f in fails[:40]:
        print("  " + f)
    if len(fails) > 40:
        print(f"  ... {len(fails)} total")
    return 1 if fails else 0


def main() -> int:
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("--public", metavar="FARM", help="verify over authenticated HTTPS endpoints")
    ap.add_argument("--base", default=PUBLIC_BASE)
    ap.add_argument("--from", dest="first", type=int)
    ap.add_argument("--to", dest="last", type=int)
    ap.add_argument("--dir", help="directory of bucket artifacts (artifact mode)")
    ap.add_argument("--tenant", help="expected tenant_id (artifact mode)")
    ap.add_argument("--allow-gaps", action="store_true",
                    help="artifact mode: the directory holds a subset of the chain (genesis plus a tail); "
                         "numbering gaps are noted, not failed")
    ap.add_argument("--bitcoin-api", default="https://blockstream.info/api",
                    help="block-header source for completed OTS proofs; pass '' to skip")
    a = ap.parse_args()
    api = a.bitcoin_api or None
    if a.public:
        return run_public(a.public, a.base, a.first, a.last, api)
    if a.dir:
        if not a.tenant:
            ap.error("--tenant is required with --dir")
        return run_dir(a.dir, a.tenant, api, allow_gaps=a.allow_gaps)
    ap.error("choose --public FARM or --dir DIR --tenant T")
    return 2


if __name__ == "__main__":
    try:
        sys.exit(main())
    except Exception:
        # Provider errors can contain tenant URLs or artifact paths.
        print('Verification could not complete; check credentials and artifact availability.')
        sys.exit(1)
