"""Freeze only closed V4 source inputs. No acquisition, source replay or release."""
from collections import Counter
from datetime import datetime, timezone
import gzip, hashlib, json, re, sqlite3, sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[3]
WORK = Path(__file__).resolve().parent
SOURCE = WORK.parent
BASE_RELEASE = 'd3881e32542e78c4b3c3e190'
PYTHON = Path('C:/Users/bhara/.cache/codex-runtimes/codex-primary-runtime/dependencies/python/python.exe')
assert Path(sys.executable).resolve() == PYTHON.resolve()
read = lambda p: json.loads(Path(p).read_text('utf-8'))

def require(ok, message):
    if not ok: raise RuntimeError(message)

def sha(p):
    h = hashlib.sha256()
    with Path(p).open('rb') as f:
        for b in iter(lambda: f.read(8 << 20), b''): h.update(b)
    return h.hexdigest()

def pin(p, expected=None):
    p = Path(p); digest = sha(p)
    require(expected is None or digest == expected, 'Closed source hash differs: ' + str(p))
    return {'path': p.relative_to(ROOT).as_posix(), 'bytes': p.stat().st_size, 'sha256': digest}

def write(p, value):
    with Path(p).open('x', encoding='utf-8') as f:
        json.dump(value, f, ensure_ascii=False, indent=2); f.write('\n')

def rows(path):
    with gzip.open(path, 'rt', encoding='utf-8') as f:
        for line in f:
            require(len(line.encode('utf-8')) <= 192 * 1024, 'Admitted source row exceeds scan bound')
            yield json.loads(line)

def closed_datacite(number, all_members):
    cohort = SOURCE / f'datacite/continuation/v2/cohorts/cohort-{number:04d}'
    cr = read(cohort / 'COHORT_RECEIPT.json')
    require(cr['ok'] is True and cr['verified_complete_members'] > 0, 'Unclosed DataCite cohort')
    dependencies = [pin(cohort / 'COHORT_RECEIPT.json'), pin(cohort / 'PLAN.json', cr['plan_sha256'])]
    specs = []
    for job, summary in sorted(cr['jobs'].items()):
        require(summary['state'] == 'VERIFIED_COMMITTED', 'Uncommitted native batch')
        d = cohort / 'batches' / job; rp = d / 'RECEIPT.json'; r = read(rp)
        deps = [pin(rp, summary['receipt_sha256'])]
        proof = read(d / 'SOURCE_REPLAY_VERIFICATION.json'); replay = read(d / 'SOURCE_REPLAY_RECEIPT.json')
        require(r['schema'] == 'ndr-datacite-batch/3' and r['batch_committed'] and not r['fixture_only'] and r['publisher_sha256_verified'], 'Native source is not committed real data')
        require(proof['batch_receipt_sha256'] == sha(rp) and not proof['fixture_only'] and
                all(proof[x] is True for x in ['artifact_hashes_verified', 'independent_recount_equal', 'source_rehashed', 'source_replayed', 'coverage_verified_against_source', 'quarantine_source_hashes_bound_by_full_replay']), 'Missing historical complete source/support proof')
        require(proof['counts'] == r['counts'] and proof['raw_source_rows'] == summary['raw_source_rows'] and
                proof['quarantined_malformed_doi_rows'] == summary['quarantined_malformed_doi_rows'], 'Native counts/quarantines differ')
        deps += [pin(d / 'SOURCE_REPLAY_VERIFICATION.json'), pin(d / 'SOURCE_REPLAY_RECEIPT.json', proof['source_replay_receipt_sha256'])]
        require(all(replay[x] == r[x] for x in ['source_sha256', 'source_bytes', 'archive_structure', 'archive_index_sha256', 'config', 'members', 'occurrences', 'counts', 'artifacts']), 'Native historical replay receipt differs')
        cfg = r['config']; require(cfg['minimum_distinct_dois'] == 2 and cfg['role_minority_ratio'] == 10 and cfg['role_minority_min_other'] == 20 and cfg['require_name_type_personal'], 'Frozen native admission gates differ')
        require(r['source_sha256'] == '678cbb64d04f24a8793f3b205ab18e5421dfdd20b89faa67fa62cce7453ec006', 'DataCite source release differs')
        for name, a in r['artifacts'].items():
            item = pin(d / name, a['sha256']); require(item['bytes'] == a['bytes'], 'Native artifact size differs'); deps.append(item)
        require(len(r['members']) == summary['member_count'], 'Native member coverage differs')
        for m in r['members']:
            require(m['name'] not in all_members, 'V4 native member overlap'); all_members.add(m['name'])
        dependencies.extend(deps)
        specs.append({'id': f'datacite-continuation-cohort-{number:04d}:{job}', 'kind': 'datacite_native',
                      'path': (d / 'names.jsonl.gz').relative_to(ROOT).as_posix(), 'sha256': r['artifacts']['names.jsonl.gz']['sha256'],
                      'receipt_path': rp.relative_to(ROOT).as_posix(), 'receipt_sha256': sha(rp),
                      'attestations_path': (d / 'attestations.jsonl.gz').relative_to(ROOT).as_posix(),
                      'attestations_sha256': r['artifacts']['attestations.jsonl.gz']['sha256'],
                      'support_proof_sha256': sha(d / 'SOURCE_REPLAY_VERIFICATION.json'),
                      'expected_roles': {'given': r['counts']['admitted_given'], 'family': r['counts']['admitted_family']},
                      'native_member_count': len(r['members']), 'scope_counts_are_not_pooled_or_people': True})
    require(sum(s['native_member_count'] for s in specs) == cr['verified_complete_members'], 'Cohort member total differs')
    return specs, dependencies

def closed_packet(lane, kind, packet):
    d = SOURCE / lane; p = d / packet; r = read(p / 'RECEIPT.json')
    require(r['complete'] and not r['fixture_only'], 'Source packet is incomplete')
    a = read(d / 'FINAL_AUDIT.json'); require(a['passed'], 'Source independent audit did not pass')
    deps = [pin(d / 'FINAL_AUDIT.json'), pin(p / 'RECEIPT.json')]
    if kind == 'orcid_minimized':
        runner = read(d / 'RUNNER_RECEIPT.json')
        require(runner['runner_pid'] is None and runner['child_pid'] is None and len(runner['history']) == 3 and
                all(x['exit_code'] == 0 for x in runner['history']) and runner['public_receipt_sha256'] == sha(p / 'RECEIPT.json'),
                'ORCID minimized receipt is not the closed preferred representation')
        deps += [pin(d / 'RUNNER_RECEIPT.json'), pin(d / 'packet-01/RECEIPT.json')]
        for name in ('CLOSED_HANDOFF.json', 'FINAL_READBACK.json', 'TERMINAL_PROCESS_OBSERVATION.json'):
            if (d / name).exists(): deps.append(pin(d / name))
        privacy = read(d / 'PUBLIC_MINIMIZATION_AUDIT.json'); require(privacy['passed'] and privacy['public_receipt_sha256'] == sha(p / 'RECEIPT.json'), 'ORCID privacy closure differs')
        require(privacy['input_exact_source_audit_sha256'] == sha(d / 'FINAL_AUDIT.json') and privacy['input_receipt_sha256'] == sha(d / 'packet-01/RECEIPT.json'), 'ORCID native/private proof linkage differs')
        deps.append(pin(d / 'PUBLIC_MINIMIZATION_AUDIT.json'))
        expected_roles = r['counts']['eligible_role_counts']
    else:
        closed = read(d / 'CLOSED_HANDOFF.json'); deps.append(pin(d / 'CLOSED_HANDOFF.json'))
        require(closed['packet_receipt_sha256'] == sha(p / 'RECEIPT.json') and a['packet_receipt_sha256'] == sha(p / 'RECEIPT.json'), 'Crossref source closure differs')
        rights = read(p / 'SOURCE_RIGHTS_REVIEW.json')
        require('not a blanket' in rights['bibliographic_facts_reuse'] and a['all_public_witnesses_match_exact_raw_response_rows'] and a['all_public_witness_keys_equal_whitelist'], 'Crossref terms/witness closure differs')
        expected_roles = r['counts']['eligible_role_counts']
    for name, item in r['artifacts'].items():
        proof = pin(p / name, item['sha256']); require(proof['bytes'] == item['bytes'], 'Packet artifact size differs'); deps.append(proof)
    return {'id': lane + ('-public02' if kind == 'orcid_minimized' else ''), 'kind': kind,
            'path': (p / 'NAMES.jsonl.gz').relative_to(ROOT).as_posix(), 'sha256': r['artifacts']['NAMES.jsonl.gz']['sha256'],
            'receipt_path': (p / 'RECEIPT.json').relative_to(ROOT).as_posix(), 'receipt_sha256': sha(p / 'RECEIPT.json'),
            'expected_roles': expected_roles, 'same_batch_count_once': True}, deps

def main():
    require(not (WORK / 'INPUT_MANIFEST.json').exists(), 'Immutable V4 input manifest already exists')
    manifest_path = ROOT / 'site/assets/public-name-data' / BASE_RELEASE / 'manifest.json'
    manifest = read(manifest_path)
    require(manifest['release'] == BASE_RELEASE and manifest['totals']['spellings'] == 911217, 'Unexpected live public baseline')
    site_files = [pin(p) for p in sorted((ROOT / 'site').rglob('*')) if p.is_file() and '.wrangler' not in p.parts]
    snapshot = {'schema': 'ndr-v4-protected-site-snapshot/1', 'files': site_files}
    if (WORK / 'BASELINE_SITE_MANIFEST.json').exists():
        require(read(WORK / 'BASELINE_SITE_MANIFEST.json') == snapshot, 'Protected baseline changed between attempts')
    else: write(WORK / 'BASELINE_SITE_MANIFEST.json', snapshot)
    dependencies = [pin(manifest_path), pin(SOURCE / 'public-expansion-v3/KEY_ROLE_INDEX_d3881e32542e78c4b3c3e190.jsonl.gz'),
                    pin(SOURCE / 'public-expansion-v3/SEGMENT_ASSET_HASHES.json'),
                    pin(SOURCE / 'public-expansion-v3/ROOT_SEGMENT_ASSET_CLOSURE.json')]
    inputs = []; members = set()
    for number in range(4, 10):
        specs, deps = closed_datacite(number, members); inputs += specs; dependencies += deps
        print(json.dumps({'stage': 'closed_cohort_pinned', 'cohort': number, 'native_batches': len(specs)}), flush=True)
    for number in (2, 3, 4):
        spec, deps = closed_packet(f'orcid-continuation-{number:02d}', 'orcid_minimized', 'packet-02'); inputs.append(spec); dependencies += deps
    for lane in ('crossref-rest-2026-10-01-03-01', 'crossref-rest-2026-10-04-06-01', 'crossref-rest-2026-10-07-09-01',
                 'crossref-rest-2026-10-07-09-frontier01', 'crossref-rest-2026-10-10-12-01'):
        spec, deps = closed_packet(lane, 'crossref_bibliographic_facts', 'packet-01'); inputs.append(spec); dependencies += deps
    result = {'schema': 'ndr-public-v4-closed-input-manifest/1', 'ok': True, 'frozen_at': datetime.now(timezone.utc).isoformat(),
        'baseline_release': BASE_RELEASE, 'baseline_keys': 911217, 'baseline_manifest_sha256': sha(manifest_path),
        'baseline_site_manifest_sha256': sha(WORK / 'BASELINE_SITE_MANIFEST.json'), 'inputs': inputs, 'dependencies': dependencies,
        'closed_native_members': len(members), 'excluded': ['ORCID05', 'ORCID06', 'DataCite10', 'DataCite11', 'all future packets'],
        'source_replay_scope': 'Rehash and reuse the exact immutable completed raw-source/support/privacy receipts; no acquisition or archive replay in this writer.',
        'crossref_is_bibliographic_facts_not_blanket_CC0': True, 'unknown_language_origin_meaning_gender_preserved': True,
        'no_canonical_site_status_docs_cache_or_LATEST_writes': True, 'checker_sha256': sha(__file__)}
    write(WORK / 'INPUT_MANIFEST.json', result)
    print(json.dumps({'ok': True, 'input_manifest_sha256': sha(WORK / 'INPUT_MANIFEST.json'), 'inputs': len(inputs), 'native_members': len(members)}), flush=True)

if __name__ == '__main__': main()
