"""Fresh research-contour snapshots with event-specific, concurrent-save routing.

Existing event prefixes and all source packages remain immutable. Legitimate
later revisions are audited as revisions, never mistaken for new distinct images
or silently appended to an already frozen model dataset.
"""
from collections import Counter, defaultdict
from contextlib import closing
import hashlib
import json
from pathlib import Path

from .cluster_targets import digest
from .mask_quality_recovery import PROJECT, PARENT, POSE, GALLERY, PUBLICATIONS, fingerprint
from .mask_quota_saved_extension import load_formal_evidence, file_inventory
from .mask_quota_completion import publication_inventory
from .mask_saved_preannotations_v2 import prelabel_registry, combine_registries, resolve_event, validate_gallery_event
from .mask_template_refined import readonly, read_jsonl, snapshot_database, QUARANTINE
from .mask_remaining_classes import original_rectangle
from .pose_review_geometry import canonical, polygon_value

BASELINE = PROJECT/'outputs/pallet_mask_quality_recovery_v2'


def read(path):
    return json.loads(Path(path).read_text())


def require(value, message):
    if not value:
        raise ValueError(message)


def raw_sha(value):
    return hashlib.sha256(value.encode('utf-8')).hexdigest()


def value_sha(value):
    return hashlib.sha256(canonical(value)).hexdigest()


def write(path, value, lines=False):
    with Path(path).open('xb') as stream:
        stream.write(b''.join(canonical(v)+b'\n' for v in value) if lines else canonical(value)+b'\n')
    Path(path).chmod(0o600)


def audit_snapshot(output):
    output = Path(output).resolve()
    require(not output.exists() and output.parent == PROJECT/'outputs', 'New private source-snapshot directory required')
    baseline = read(BASELINE/'result.json'); source_seal = read(BASELINE/'independent_verification.json')
    require(digest(BASELINE/'independent_verification.json') == '61b73d8e0dbc7b99db01b75e912c797beacc34c8c0b3d31434058eac3d376428'
        and source_seal['result_sha256'] == digest(BASELINE/'result.json'), 'Baseline seal changed')
    require(fingerprint(POSE) == baseline['source_fingerprints']['pose'], 'Pose-store updates require a separate provenance refresh')
    config = file_inventory(PROJECT/'configs')
    output.mkdir(mode=0o700); private = output/'private'; private.mkdir(mode=0o700)
    snapshot_database(POSE, private/'pose_snapshot.sqlite3')
    snapshot_database(GALLERY, private/'gallery_snapshot.sqlite3')
    manifest = read(PARENT/'manifest.json')
    formal, rows, evidence = load_formal_evidence(manifest)
    publications = publication_inventory({v: PROJECT/'outputs'/name for v, name in PUBLICATIONS.items()}, manifest['formal_manifest_sha256'])
    with closing(readonly(private/'pose_snapshot.sqlite3', True)) as db:
        pose_current = {k: json.loads(raw) for k, raw in db.execute('SELECT target_key,payload FROM annotations')}
        pose_event_keys = {k for k, in db.execute('SELECT DISTINCT target_key FROM events')}
    task_paths = [(PROJECT/f'outputs/pallet_mask_preannotated_requests_v{n}', version, f'v{n}')
        for n, version in ((1, 13), (2, 14), (3, 15))]
    for directory in sorted((PROJECT/'outputs').glob('pallet_mask_under50_requests_v*')):
        if (directory/'verification.json').is_file() and (directory/'independent_verification.json').is_file():
            task_paths.append((directory, 15, directory.name))
    registries = [prelabel_registry(path, rows, manifest['formal_manifest_sha256'], publications[version],
        pose_current, pose_event_keys, version, namespace) for path, version, namespace in task_paths]
    registry = combine_registries(*registries)
    with closing(readonly(BASELINE/'private/gallery_snapshot.sqlite3', True)) as db:
        old_events = list(db.execute('SELECT id,target_key,revision,payload FROM events ORDER BY id'))
        old_current = {k: raw for k, raw in db.execute('SELECT target_key,payload FROM reviews')}
    histories = defaultdict(list); cache = {}; catalog = [r for r in read_jsonl(BASELINE/'private/source_catalog.jsonl') if r['origin'] == 'saved_pose']
    audit = []; current = {}; events = []
    with closing(readonly(private/'gallery_snapshot.sqlite3', True)) as db:
        events = list(db.execute('SELECT id,target_key,revision,payload FROM events ORDER BY id'))
        require(events[:len(old_events)] == old_events, 'Baseline gallery raw event prefix changed')
        for event_id, key, revision, raw in events:
            value = json.loads(raw)
            reference = resolve_event(value, rows, manifest['formal_manifest_sha256'], publications, registry, cache)
            validate_gallery_event(value, rows[key], reference, revision)
            require(value['proposal_polygon'] == reference['proposal']['polygon'], 'Event proposal polygon differs from its bound package')
            histories[key].append((event_id, revision, raw, reference))
            audit.append(dict(event_id=event_id, target_key=key, revision=revision, payload_raw_sha256=raw_sha(raw),
                proposal_publication=value['proposal_publication'], base_proposal_id=value['base_proposal_id'],
                namespace=reference.get('namespace'), event_specific_source_verified=True))
        for key, revision, raw in db.execute('SELECT target_key,revision,payload FROM reviews ORDER BY target_key'):
            history = histories[key]; value = json.loads(raw)
            require(history[-1][1:3] == (revision, raw) and len({h[1] for h in history}) == len(history), 'Current is not exact raw unique latest event')
            require(key not in QUARANTINE and not original_rectangle(value['polygon'], rows[key]), 'Quarantined/original rectangle not a research contour')
            polygon_value(value['polygon'], *rows[key]['size'])
            if key in old_current:
                require(any(h[2] == old_current[key] for h in history), 'Prior current raw revision is no longer recoverable')
            current[key] = raw
            reference = history[-1][3]
            catalog.append(dict(target_key=key, row=rows[key], polygon=value['polygon'], origin='saved_gallery', revision=revision,
                payload_sha256=value_sha(value), event_raw_sha256=raw_sha(raw),
                initial_polygon=json.loads(history[0][2])['proposal_polygon'],
                batch=reference.get('namespace') or 'historical_gallery', history_revisions=len(history),
                actual_saved_research_reference_not_GT=True))
    require(set(old_current) <= set(current), 'Old saved contours disappeared')
    require(len(catalog) == len({r['target_key'] for r in catalog}) == len({r['row']['frame_id'] for r in catalog}), 'Repeated-image contour count')
    changed = [k for k in old_current if current[k] != old_current[k]]
    new_keys = sorted(set(current)-set(old_current))
    records_path = PROJECT/'outputs/pallet_mask_model_training_v1/private/training_records.jsonl'
    trained = {r['target_key']: r['development_split'] for r in read_jsonl(records_path)} if records_path.exists() else {}
    summary = dict(state='passed_event_provenance_and_distinct_saved_count_not_contour_accuracy',
        total_contours=len(catalog), gallery_contours=len(current), gallery_events=len(events), pose_contours=482,
        class_counts=dict(Counter(r['row']['final_class_id'] for r in catalog)),
        old_raw_event_prefix_preserved=len(old_events), changed_baseline_current_keys=changed,
        new_distinct_saved_keys=new_keys, actual_changed_baseline_revisions_not_new_distinct_images=True,
        baseline_current_revisions_preserved_in_history=True,
        trained_reference_keys_with_newer_human_revision={k: trained[k] for k in changed if k in trained},
        frozen_training_dataset_modified=False, source_registry_task_manifests={namespace: digest(path/'manifest.json') for path, _, namespace in task_paths},
        pose_fingerprint=fingerprint(private/'pose_snapshot.sqlite3'), gallery_fingerprint=fingerprint(private/'gallery_snapshot.sqlite3'),
        configuration_sha256=config, source_catalog_sha256=None, production_annotation_writes=0,
        code_sha256=digest(__file__))
    write(private/'source_catalog.jsonl', catalog, True)
    write(private/'event_binding_audit.jsonl', audit, True)
    summary['source_catalog_sha256'] = digest(private/'source_catalog.jsonl')
    write(output/'audit.json', summary)
    require(file_inventory(PROJECT/'configs') == config, 'Global configs changed during source audit')
    print(json.dumps({k: v for k, v in summary.items() if k in ('state', 'total_contours', 'gallery_events', 'new_distinct_saved_keys', 'trained_reference_keys_with_newer_human_revision')}), flush=True)
    return formal, rows, evidence, catalog, summary


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--output', type=Path, required=True)
    args = parser.parse_args(); audit_snapshot(args.output)
