"""Private disk-backed merge of already-approved public observations.

No acquisition/deployment, source-packet execution, private profile locators or
publication-pointer writes. Only closed, hash-bound inputs may enter a unit.
"""
import gzip
import hashlib
import json
from pathlib import Path
import sqlite3

import build_public_name_directory as base
import build_public_name_expansion as v2

ROOT = base.ROOT
WORK = ROOT / "outputs/public-name-expansion-2026-11-01/public-expansion-v3"
BASE_RELEASE = "9f00597ac515846bdf97e072"


def lines(path):
    with gzip.open(path, "rt", encoding="utf-8") as stream:
        for line in stream:
            yield json.loads(line)


def project(row, scope, normalizer):
    if scope.startswith("datacite-continuation-cohort-"):
        declared = row["provenance"]["source_scope_id"]
        base.require(declared.startswith(scope + ":"), "Unexpected native source scope")
        return v2.event(row, declared, normalizer)
    if scope == "dictionary-tranche03":
        return v2.event(row, scope, normalizer)
    base.require(scope == "orcid-continuation-01-public02", "Unexpected publication input")
    proof = row["provenance"]
    role, form, key = row["role"], row["form"], row["key"]
    base.require(role in {"given", "family"} and normalizer.name_key(form) == key and
                 normalizer.hygiene_reason(normalizer.normalize_form(form)) is None and row["language"] is None and
                 proof["license"] == "CC0-1.0" and proof["self_asserted_public_name_field"] is True and
                 proof["primary_field"] == ("given-names" if role == "given" else "family-name") and
                 isinstance(row["distinct_support_records"], int) and row["distinct_support_records"] >= 2,
                 "Invalid minimized ORCID assertion")
    # Explicit public whitelist. No ORCID IDs, contact details, per-profile hashes,
    # private audit paths, support_examples or profile/member locators survive.
    return {"source_id": "orcid-public-2026", "tier": "bibliographic", "evidence_kind": "public_profile_summary",
            "role": role, "form": form, "language": None, "cohort": scope,
            "primary_field": proof["primary_field"], "distinct_public_record_support": row["distinct_support_records"]}


class Catalog:
    def __init__(self, path):
        self.path = Path(path)
        self.path.parent.mkdir(parents=True, exist_ok=True)
        self.db = sqlite3.connect(self.path)
        self.db.executescript("""
            PRAGMA journal_mode=WAL;
            PRAGMA synchronous=NORMAL;
            PRAGMA cache_size=-65536;
            PRAGMA mmap_size=0;
            CREATE TABLE IF NOT EXISTS names(key TEXT PRIMARY KEY,name TEXT NOT NULL,base_json TEXT);
            CREATE TABLE IF NOT EXISTS forms(key TEXT,form TEXT,PRIMARY KEY(key,form)) WITHOUT ROWID;
            CREATE TABLE IF NOT EXISTS roles(key TEXT,role TEXT,PRIMARY KEY(key,role)) WITHOUT ROWID;
            CREATE TABLE IF NOT EXISTS source_roles(key TEXT,source_id TEXT,role TEXT,PRIMARY KEY(key,source_id,role)) WITHOUT ROWID;
            CREATE TABLE IF NOT EXISTS events(seq INTEGER PRIMARY KEY,key TEXT,source_id TEXT,data TEXT,event_sha TEXT UNIQUE);
            CREATE TABLE IF NOT EXISTS units(id TEXT PRIMARY KEY,sha256 TEXT,rows INTEGER);
        """)
        if "priority" not in {r[1] for r in self.db.execute("PRAGMA table_info(names)")}:
            self.db.execute("ALTER TABLE names ADD COLUMN priority INTEGER NOT NULL DEFAULT 0")
            self.db.execute("UPDATE names SET priority=10 WHERE base_json IS NULL")
        if "unit_rank" not in {r[1] for r in self.db.execute("PRAGMA table_info(events)")}:
            self.db.execute("ALTER TABLE events ADD COLUMN unit_rank INTEGER NOT NULL DEFAULT 0")
            self.db.execute("UPDATE events SET unit_rank=10 WHERE data LIKE ?", ('{"cohort":"datacite-continuation-cohort-0001:%',))
        self.db.commit()

    def done(self, unit, fingerprint):
        row = self.db.execute("SELECT sha256,rows FROM units WHERE id=?", (unit,)).fetchone()
        if row:
            base.require(row[0] == fingerprint, "Previously imported immutable input changed")
            return row[1]
        return None

    def event(self, key, form, data):
        scope = data.get("cohort", "")
        rank = next((10 * n for n in (1, 2, 3) if scope.startswith(f"datacite-continuation-cohort-{n:04d}:")),
                    40 if scope == "dictionary-tranche03" else 50 if scope == "orcid-continuation-01-public02" else 0)
        self.db.execute("INSERT INTO names(key,name,base_json,priority) VALUES(?,?,NULL,?) ON CONFLICT(key) DO UPDATE SET name=excluded.name,priority=excluded.priority WHERE names.base_json IS NULL AND excluded.priority<names.priority", (key, form, rank))
        self.db.execute("INSERT OR IGNORE INTO forms VALUES(?,?)", (key, form))
        self.db.execute("INSERT OR IGNORE INTO roles VALUES(?,?)", (key, data["role"]))
        self.db.execute("INSERT OR IGNORE INTO source_roles VALUES(?,?,?)", (key, data["source_id"], data["role"]))
        encoded = base.encoded(data).decode("utf-8").rstrip("\n")
        digest = hashlib.sha256(base.encoded({"key": key, "event": data})).hexdigest()
        self.db.execute("INSERT OR IGNORE INTO events(key,source_id,data,event_sha,unit_rank) VALUES(?,?,?,?,?)", (key, data["source_id"], encoded, digest, rank))

    def baseline(self):
        manifest_path = ROOT / "site/assets/public-name-data" / BASE_RELEASE / "manifest.json"
        manifest = json.loads(manifest_path.read_text("utf-8"))
        base.require(manifest["release"] == BASE_RELEASE and manifest["totals"]["spellings"] == 597769, "Unexpected live baseline")
        fingerprint = base.sha(manifest_path)
        if self.done("baseline597769", fingerprint) is not None:
            return manifest
        count = 0
        for bucket in manifest["views"]["all"]["buckets"]:
            p = ROOT / "site" / bucket["path"].lstrip("/")
            base.require(base.sha(p) == bucket["sha256"], "Baseline index changed")
            index = json.loads(p.read_text("utf-8"))
            for page in index["pages"]:
                p = ROOT / "site" / page["path"].lstrip("/")
                base.require(base.sha(p) == page["sha256"], "Baseline page changed")
                raw = gzip.decompress(p.read_bytes())
                base.require(len(raw) <= 256 * 1024, "Baseline inflated page exceeds bound")
                payload = json.loads(raw)
                base.require(payload["release"] == BASE_RELEASE and len(payload["records"]) == page["count"], "Invalid baseline page")
                for row in payload["records"]:
                    retained = {k: v for k, v in row.items() if k not in {"attestations", "source_role_difference"}}
                    self.db.execute("INSERT OR IGNORE INTO names(key,name,base_json) VALUES(?,?,?)", (row["key"], row["name"], base.encoded(retained).decode("utf-8")))
                    for form in row["forms"]:
                        self.db.execute("INSERT OR IGNORE INTO forms VALUES(?,?)", (row["key"], form))
                    for role in row["roles"]:
                        self.db.execute("INSERT OR IGNORE INTO roles VALUES(?,?)", (row["key"], role))
                    for locator in row["primary_locators"]:
                        self.db.execute("INSERT OR IGNORE INTO source_roles VALUES(?,?,?)", (row["key"], row["source_id"], locator["role"]))
                    for item in row.get("attestations", []):
                        self.event(row["key"], item["form"], item)
                    count += 1
                    if count % 10000 == 0:
                        self.db.commit()
        base.require(count == 597769, "Baseline source keys changed")
        self.db.execute("INSERT INTO units VALUES(?,?,?)", ("baseline597769", fingerprint, count))
        self.db.commit()
        self.db.execute("PRAGMA wal_checkpoint(TRUNCATE)")
        return manifest

    def add(self, path, scope, fingerprint, expected_rows):
        path = Path(path)
        base.require(base.sha(path) == fingerprint, "Closed source input changed")
        prior = self.done(scope, fingerprint)
        if prior is not None:
            base.require(prior == expected_rows, "Previously imported input count changed")
            return prior
        normalizer = base.policy()
        count = 0
        for row in lines(path):
            data = project(row, scope, normalizer)
            self.event(row["key"], row["form"], data)
            count += 1
            if count % 10000 == 0:
                self.db.commit()
        base.require(count == expected_rows, "Closed admitted input row count differs")
        self.db.execute("INSERT INTO units VALUES(?,?,?)", (scope, fingerprint, count))
        self.db.commit()
        self.db.execute("PRAGMA wal_checkpoint(TRUNCATE)")
        return count

    def records(self):
        self.db.execute("CREATE INDEX IF NOT EXISTS events_by_key ON events(key,seq)")
        self.db.commit()
        for key, name, retained in self.db.execute("SELECT key,name,base_json FROM names ORDER BY key"):
            row = json.loads(retained) if retained else {"key": key, "name": name, "source_id": None,
                "source_category": "public_source_attestations", "evidence_status": "source_attested",
                "corroborated_roles": [], "role_warnings": [], "language": None,
                "written_script": base.written_script(name), "primary_locators": [], "corroboration": []}
            row["forms"] = [x[0] for x in self.db.execute("SELECT form FROM forms WHERE key=? ORDER BY form", (key,))]
            row["roles"] = [x[0] for x in self.db.execute("SELECT role FROM roles WHERE key=? ORDER BY role", (key,))]
            events = [json.loads(x[0]) for x in self.db.execute("SELECT data FROM events WHERE key=? ORDER BY unit_rank,seq", (key,))]
            if events:
                row["attestations"] = events
            source_roles = {}
            for source, role in self.db.execute("SELECT source_id,role FROM source_roles WHERE key=? ORDER BY source_id,role", (key,)):
                source_roles.setdefault(source, []).append(role)
            row["source_role_difference"] = len({tuple(value) for value in source_roles.values()}) > 1
            yield row

    def close(self):
        self.db.close()
