"""Post-deletion verification without importing the checkpoint pruner.

Writes only a new verification record after the producer completes. Does not
claim to re-read deleted bytes or to retain optimizer/RNG continuation state.
"""
from __future__ import annotations

from collections import Counter, defaultdict
from contextlib import closing
from datetime import datetime, timezone
import hashlib
import json
from pathlib import Path
import shutil
import sqlite3
import sys
import urllib.request

PROJECT = Path(__file__).resolve().parents[1]
ROOT_NAME = 'outputs/pallet_pose_benchmark_v4'
OUTPUT_NAME = 'outputs/pallet_pose_v4_checkpoint_cleanup_v1'


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


def canonical(value):
    return json.dumps(value, sort_keys=True, separators=(',', ':'), allow_nan=False).encode()


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


def digest(path):
    with Path(path).open('rb') as stream:
        return hashlib.file_digest(stream, 'sha256').hexdigest()


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


def fingerprint(path):
    with closing(sqlite3.connect(path.resolve().as_uri()+'?mode=ro', uri=True)) as db:
        db.execute('PRAGMA query_only=ON'); db.execute('BEGIN')
        schema = list(db.execute('SELECT type,name,tbl_name,sql FROM sqlite_master ORDER BY type,name'))
        tables = {}
        for kind, name, *_ in schema:
            if kind == 'table':
                require(name.replace('_', '').isalnum(), 'Unknown SQL table name')
                tables[name] = sha(sorted(canonical(list(row)).decode() for row in db.execute('SELECT * FROM "'+name+'"')))
        return dict(schema_sha256=sha(schema), tables=tables)


def stat_value(path):
    st = path.stat()
    return dict(bytes=st.st_size, allocated_bytes=st.st_blocks*512, device=st.st_dev,
        inode=st.st_ino, links=st.st_nlink, mtime_ns=st.st_mtime_ns, mode=st.st_mode & 0o777)


def verify(out=None, check_services=True):
    out = Path(out or PROJECT/OUTPUT_NAME).absolute()
    require(out.parent == PROJECT/'outputs' and not out.is_symlink(), 'Use completed outputs audit directory')
    require(not (out/'post_cleanup_verification.json').exists(), 'Preserve existing verification')
    result, plan, before = (read(out/name) for name in ('result.json', 'plan.json', 'protection_before.json'))
    require(result['state'] == 'passed' and digest(out/'plan.json') == result['plan_sha256'], 'Incomplete or changed cleanup')
    require(digest(out/'protection_before.json') == plan['protection_before_sha256'], 'Pre-cleanup evidence changed')
    root = PROJECT/ROOT_NAME; index = read(out/'best_weights_index.json')
    require(index['models'] == plan['retained_best_models'], 'Best-weight index differs from approved plan')
    keep = {r['checkpoint']: r for r in index['models']}
    require(len(keep) == 16 and len(index['models']) == 16, 'Expected eight families and two separate unit branches')
    targets = {r['path']: r for r in plan['candidates']}
    require(len(targets) == len(plan['candidates']) == result['deleted_files'] == 368, 'Unexpected deletion count')
    require(not targets.keys() & keep.keys(), 'Best model was a deletion target')
    journal = [json.loads(line) for line in (out/'deletion_journal.jsonl').read_text().splitlines()]
    removed = [r['path'] for r in journal if r['state'] == 'deleted']
    require(len(removed) == len(set(removed)) == 368 and set(removed) == set(targets), 'Journal mismatch')
    for path, record in targets.items():
        p = PROJECT/path
        require(not p.exists() and not p.is_symlink(), 'Deleted checkpoint still exists')
        require(path.startswith((ROOT_NAME+'/trials/', ROOT_NAME+'/refit/')) and path.endswith('.pt'), 'Deletion outside training scope')
        completed = read(PROJECT/record['completed_record'])
        require(digest(PROJECT/record['completed_record']) == record['completed_record_sha256'], 'Original completed record changed')
        if p.name in ('last.pt', 'selected.pt'):
            require(record['sha256'] == completed['last_sha256' if p.name == 'last.pt' else 'selected_sha256'], 'Recorded deletion hash differs from history')
    require(Counter(c['category'] for c in targets.values()) == Counter({'last.pt': 178, 'selected.pt': 178, 'nonwinning_refit_epoch.pt': 12}), 'Wrong checkpoint categories')
    selection = read(root/'selection_frozen.json'); config = read(PROJECT/'configs/pallet_pose_benchmark_v4.json')
    require(selection['val_test_used'] is False, 'Holdout-based best selection is not permitted')
    require(digest(root/'selection_frozen.json') == plan['selection_sha256'] == index['source_selection_sha256'], 'Frozen selection changed')
    expected = {}
    for branch, profile in selection['profiles'].items():
        families = defaultdict(list)
        for entry in profile['entries']:
            families[entry['family']].append(entry)
        require(set(families) == set(config['families']), 'Missing model family')
        for family, entries in families.items():
            chosen = min(entries, key=lambda e: e['mean_score'])
            profile_record = next(r for r in read(root/'refit'/family/branch/'profiles.json') if r['arm'] == chosen['arm'])
            path = ROOT_NAME+'/'+profile_record['checkpoint']; expected[path] = profile_record['checkpoint_sha256']
            require(path in keep and keep[path]['arm'] == chosen['arm'] and keep[path]['refit_epoch'] == chosen['refit_epoch'], 'Not the frozen CV best refit')
            require(profile_record['refit_epoch'] == chosen['refit_epoch'], 'Epoch mismatch')
    require(set(expected) == set(keep), 'Unexpected best-model keep set')
    require(ROOT_NAME+'/refit/pointnext/metric/long_b32/epochs/180.pt' in keep, 'V5 dependency missing')
    actual = {str(p.relative_to(PROJECT)) for tree in ('trials', 'refit') for p in (root/tree).rglob('*.pt')}
    require(actual == set(keep), 'Training worktree contains non-best or missing checkpoints')
    import torch
    for path, expected_sha in expected.items():
        require(digest(PROJECT/path) == expected_sha == keep[path]['checkpoint_sha256'], 'Best checkpoint bytes changed')
        state = torch.load(PROJECT/path, map_location='cpu', weights_only=True)
        require(state['family'] == keep[path]['family'] and state['selected_epoch'] == keep[path]['refit_epoch'], 'Best checkpoint metadata changed')
        require(state['frozen_encoder'] == keep[path]['frozen_encoder'], 'Encoder/head semantics changed')
        require(state['model'] and all(isinstance(t, torch.Tensor) and torch.isfinite(t).all().item() for t in state['model'].values()), 'Invalid best model tensors')
    protected = before['v4_retained_files_sha256']
    actual_files = {str(p.relative_to(PROJECT)) for p in root.rglob('*') if p.is_file() and not p.is_symlink()}
    require(actual_files == set(protected), 'Retained v4 file inventory changed')
    for path, expected_sha in protected.items():
        require(digest(PROJECT/path) == expected_sha, 'Retained v4 artifact changed: '+path)
    unrelated = before['unrelated']
    configs = {str(p.relative_to(PROJECT)): digest(p) for p in (PROJECT/'configs').rglob('*') if p.is_file()}
    require(configs == unrelated['configurations_sha256'], 'Active or historical configuration changed')
    for path, expected_fingerprint in unrelated['human_database_logical_fingerprints'].items():
        require(fingerprint(PROJECT/path) == expected_fingerprint, 'Human database content changed')
    formal = {str(p.relative_to(PROJECT)): stat_value(p) for p in (PROJECT/'outputs/pallet_dataset_formal_v1/dataset').rglob('*') if p.is_file()}
    require(formal == unrelated['formal_file_stat_identity'], 'Formal file namespace/stat identity changed')
    health = {}
    if check_services:
        for name, port in (('mask_gallery', 3393), ('pose_review', 3390), ('structure_review', 3389)):
            with urllib.request.urlopen(f'http://127.0.0.1:{port}/healthz', timeout=15) as response:
                json.loads(response.read()); require(response.status == 200, 'Service unhealthy')
                health[name] = response.status
    value = dict(state='passed', plan_sha256=result['plan_sha256'], verifier_sha256=digest(__file__),
        removed_files_and_journal_verified=368, frozen_cv_best_training_weights_verified=16,
        retained_v4_artifact_content_hashes_verified=len(protected), formal_file_stat_identities_verified=len(formal),
        human_databases_logically_unchanged=3, configurations_unchanged=True,
        full_original_deployment_and_ZIPs_preserved=True, v5_pointnext_checkpoint_preserved=True,
        service_health_http_status=health, current_free_bytes=shutil.disk_usage(PROJECT/'outputs').free,
        removed_allocated_bytes=result['removed_allocated_bytes'], measured_net_free_increase_bytes=result['measured_filesystem_free_increase_bytes'],
        scope='post-deletion absence/journal, frozen choices, retained byte hashes and CPU model loads; not an independent reread of deleted bytes',
        original_optimizer_RNG_state_available=False, original_full_checkpoint_audit_rerunnable=False,
        training_or_accuracy_evaluation_launched=False, checked_at=datetime.now(timezone.utc).isoformat())
    with (out/'post_cleanup_verification.json').open('xb') as stream:
        stream.write(canonical(value)+b'\n')
    print(json.dumps(value), flush=True)
    return value


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--directory', type=Path, default=PROJECT/OUTPUT_NAME)
    args = parser.parse_args()
    verify(args.directory)
