#!/usr/bin/env python3
"""Offline consistency check of PUBLIC KoreAves release outputs, no raw records.

This checks whether 617 versioned public pages/profiles, record-source ledger,
and published citation manifest agree. It does not independently reconstruct
the provider observation dataset or validate biological identifications.
Usage: python3 verify_evidence_snapshot.py ./downloaded-koreaves-static-export
"""
from __future__ import annotations

import argparse
import collections
import json
import re
import tempfile
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from urllib.parse import urlsplit
from urllib.request import Request, urlopen


def read(base: Path, relative: str):
    return json.loads((base / relative).read_text(encoding="utf-8"))


def verify(base: Path) -> dict:
    base = base.resolve()
    manifest = read(base, "data/koreaves-evidence-release.json")
    catalog = read(base, "data/runtime/v1/catalog.json")
    ledger = read(base, "data/derived/evidence_explorer/v1/index.json")
    taxa = {x["canonical_taxon_id"]: x for x in manifest["taxa"]}
    profiles = {x["id"]: x for x in catalog["taxa"]}
    rows = {x["canonical_taxon_id"]: x for x in ledger["taxa"]}
    if len(taxa) != 617 or len(profiles) != 617 or len(rows) != 617:
        raise ValueError("Public catalog, evidence and manifest require 617 unique reference taxa")
    if set(taxa) != set(profiles) or set(taxa) != set(rows):
        raise ValueError("Public source identity mismatch")
    if manifest["dataset"]["canonical_taxon_count"] != 617 or manifest["dataset"]["region_count"] != 17:
        raise ValueError("Published manifest scope inconsistent")
    if manifest["rights"]["dataset_license"] is not None:
        raise ValueError("Unexpected global license; individual source rights must be reviewed")
    contract = manifest.get("publication_contract") or {}
    for name in ("raw_records_emitted", "coordinates_emitted", "provider_record_identifiers_emitted", "source_archives_emitted"):
        if contract.get(name) is not False:
            raise ValueError(f"Public-only rights contract violated: {name}")
    count = collections.Counter()
    for source in manifest["sources"]["record_sources"]:
        count["source_input_rows"] += source["normalized_record_rows"]
    if count["source_input_rows"] != manifest["sources"]["normalized_record_rows_total"]:
        raise ValueError("Source record totals in citation manifest do not reconcile")
    for cid in sorted(taxa):
        item = taxa[cid]
        overview = profiles[cid]
        if overview["sci"] != item["identity"]["scientific_name"]:
            raise ValueError(f"Identity mismatch for {cid}")
        if overview["rank"] != item["identity"]["rank"]:
            raise ValueError(f"Rank mismatch for {cid}")
        shard = read(base, f"data/runtime/v1/taxa/{cid.rsplit(':',1)[-1]}.json")
        if shard["canonical_taxon_id"] != cid or shard["scientific_name"] != overview["sci"]:
            raise ValueError(f"Public profile identity mismatch for {cid}")
        evidence = shard["objects"]["korea_presence_evidence"]
        reg = shard["objects"]["regional_evidence_footprint"]["fused"]
        scoped = item["regional_evidence"]
        if scoped["documented_region_count"] != reg["region_count"] or scoped["documented_region_count"] != overview["regionCount"]:
            raise ValueError(f"Regional count mismatch for {cid}")
        places = {r["sido_nm"] for r in reg["regions_with_evidence"]}
        if places != set(scoped["regions"]):
            raise ValueError(f"Regional source place-set mismatch for {cid}")
        source_types = sorted(evidence["claim_support"]["supporting_source_types"])
        if source_types != sorted(item["source_support"]["supporting_source_types"]):
            raise ValueError(f"Empirical source type mismatch for {cid}")
        if source_types != sorted(overview["supportingSourceTypes"]):
            raise ValueError(f"Catalog source support mismatch for {cid}")
        for source in ("gbif", "ecobank"):
            n = evidence["mapped_record_count_by_source"][source]
            if n != rows[cid]["sources"][source]["linked"]:
                raise ValueError(f"Taxon/source ledger discrepancy for {cid}/{source}")
            count[f"{source}_mapped_rows"] += n
            if n > 0 and source not in source_types:
                raise ValueError(f"Missing evidence source claim for {cid}/{source}")
        proof = (shard.get("decision_trace") or {}).get("gbif")
        if proof:
            paths = proof.get("mapping_proof_paths") or []
            total = sum(x["rows"] for x in paths) + proof.get("additional_proof_path_rows", 0)
            if total != evidence["mapped_record_count_by_source"]["gbif"]:
                raise ValueError(f"Taxon-level GBIF source concept proof mismatch for {cid}")
            count["gbif_source_concept_proof_paths"] += len(paths)
        if item["conservation"]["availability"] != shard["conservation_summary"]["availability"]:
            raise ValueError(f"Unattributed conservation field for {cid}")
        count["taxa"] += 1
        count["documented_taxon_regions"] += scoped["documented_region_count"]
    return {
        "status": "PASS_PUBLIC_OUTPUT_RECONCILIATION_ONLY",
        "evidence_version": manifest["dataset"]["version"],
        "semantic_content_sha256_claimed": manifest["dataset"]["content_sha256"],
        "totals": dict(sorted(count.items())),
        "scope": "public KBR-indexed source rows, not individual birds",
        "reproducibility_level": "R2_public_output_crosschecks",
        "not_verified": [
            "Recreation of provider raw GBIF/EcoBank original observations",
            "Historical normalized source concept ID regeneration",
            "Independent taxonomic or biological identification",
            "Public redistribution rights of underlying source records",
        ],
    }


def fetch_public_snapshot(site_url: str, output: Path) -> None:
    """Download only rights-cleared static public projections from KoreAves.

    This acts on the user's machine, never on a KoreAves Pages Function.
    Fixed host/scheme and a bounded list of taxon JSON shards prevent a
    provider/source URL in a JSON payload from becoming an arbitrary fetch.
    """
    parsed = urlsplit(site_url)
    host = (parsed.hostname or "").lower()
    allowed = host == "koreaves.com" or bool(re.fullmatch(
        r"[a-z0-9-]+\\.koreaves\\.pages\\.dev", host
    ))
    if parsed.scheme != "https" or not allowed or parsed.port or parsed.username or parsed.password or parsed.path not in ("", "/") or parsed.query or parsed.fragment:
        raise ValueError("Only the KoreAves public HTTPS site or its immutable Cloudflare Pages release is permitted")
    base = f"https://{host}"
    seed = [
        "data/koreaves-evidence-release.json",
        "data/runtime/v1/catalog.json",
        "data/derived/evidence_explorer/v1/index.json",
    ]

    def fetch(rel):
        req = Request(f"{base}/{rel}", headers={"User-Agent": "KoreAves-public-output-verifier/1.0"})
        with urlopen(req, timeout=20) as response:
            landing = urlsplit(response.geturl())
            if landing.scheme != "https" or landing.hostname != host:
                raise ValueError("Unexpected cross-origin download redirect")
            payload = response.read(2_000_001)
            if len(payload) > 2_000_000:
                raise ValueError("Unexpectedly large public verification file")
        target = output / rel
        target.parent.mkdir(parents=True, exist_ok=True)
        target.write_bytes(payload)

    for path in seed:
        fetch(path)
    catalog = read(output, "data/runtime/v1/catalog.json")
    identities = [item.get("id") for item in catalog.get("taxa", [])]
    if len(identities) != 617 or len(set(identities)) != 617 or any(
        not isinstance(x, str) or not re.fullmatch(r"koreaves:taxon:[0-9a-f]{26}", x)
        for x in identities
    ):
        raise ValueError("Public catalog does not contain exactly 617 valid taxon keys")
    files = [f"data/runtime/v1/taxa/{cid.rsplit(':',1)[-1]}.json" for cid in identities]
    with ThreadPoolExecutor(max_workers=6) as pool:
        list(pool.map(fetch, files))


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("static_root", type=Path, nargs="?", help="Folder containing the downloaded public /data/... hierarchy")
    parser.add_argument("--fetch", metavar="HTTPS_SITE", help="Download and verify the bounded public files directly from KoreAves")
    args = parser.parse_args()
    if bool(args.fetch) == bool(args.static_root):
        parser.error("Supply exactly one of static_root or --fetch")
    if args.fetch:
        with tempfile.TemporaryDirectory(prefix="koreaves-public-verify-") as scratch:
            fetch_public_snapshot(args.fetch, Path(scratch))
            result = verify(Path(scratch))
    else:
        result = verify(args.static_root)
    print(json.dumps(result, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
