"""Read-only, exhaustive remaining-under50 capacity after a saved worklist.

Only a fresh audit directory is written. This does not relax quality gates,
reissue rejected images, create a worklist, or touch production annotation data.
"""
from collections import Counter, defaultdict
from contextlib import closing
from datetime import datetime, timezone
import json
from pathlib import Path
import sys

PROJECT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT))
from pallet.cluster_targets import digest, protected_snapshot
from pallet.mask_quality_recovery import POSE, GALLERY, fingerprint
from pallet.mask_quota_saved_extension import file_inventory, load_formal_evidence
from pallet.mask_remaining_classes import confirmed, original_rectangle
from pallet.mask_template_refined import QUARANTINE, readonly, read_jsonl
from pallet.pose_review_geometry import canonical
from scripts.build_mask_template_requests import native_quality

PARENT = PROJECT/'outputs/pallet_mask_remaining_round5_v1'


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


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


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


def remaining_gaps(catalog):
    keys = [r['target_key'] for r in catalog]
    frames = [r['row']['frame_id'] for r in catalog]
    require(len(keys) == len(set(keys)) == len(set(frames)), 'Real-save count must use distinct targets and images')
    counts = Counter(r['row']['final_class_id'] for r in catalog)
    return counts, {c: 50-counts[c] for c in range(1, 23) if counts[c] < 50}


def classify(row, key, protected, frames, image_hashes, evidence, image_digest):
    # Do not inspect a protected image again or weaken its prior disposition.
    if key in protected:
        return 'protected_target', None
    if row['frame_id'] in frames:
        return 'protected_frame', None
    quality, reason = native_quality(row, evidence)
    if reason:
        return reason, None
    if image_digest(row) in image_hashes:
        return 'duplicate_protected_native_image_bytes', quality
    return 'new_native_quality_candidate_pending_visual_inspection', quality


def audit(snapshot, pack, output):
    snapshot, pack, output = (Path(p).resolve() for p in (snapshot, pack, output))
    require(output.parent == PROJECT/'outputs' and not output.exists(), 'Fresh audit output required')
    require(snapshot.parent == pack.parent == PROJECT/'outputs', 'Explicit local source and task directories required')
    summary = read(snapshot/'audit.json')
    require(summary['state'] == 'passed_event_provenance_and_distinct_saved_count_not_contour_accuracy'
        and summary['source_catalog_sha256'] == digest(snapshot/'private/source_catalog.jsonl'), 'Verified saved-source binding required')
    require(summary['code_sha256'] == digest(PROJECT/'pallet/mask_review_continuation.py'), 'Source audit code changed')
    config_before = file_inventory(PROJECT/'configs')
    require(config_before == summary['configuration_sha256'], 'Source audit configurations differ')
    for kind, path in (('pose', POSE), ('gallery', GALLERY)):
        require(fingerprint(path) == summary[kind+'_fingerprint'], 'Human sources advanced; create a fresh snapshot')
    catalog = read_jsonl(snapshot/'private/source_catalog.jsonl'); counts, gaps = remaining_gaps(catalog)
    require(summary['class_counts'] == {str(c): n for c, n in counts.items()}, 'Saved count summary differs')
    manifest = read(PARENT/'manifest.json'); formal, rows, evidence = load_formal_evidence(manifest)
    formal_before = protected_snapshot(formal)
    completed_manifest, seal = read(pack/'manifest.json'), read(pack/'verification.json')
    require((pack/'verification.json').read_bytes() == (pack/'independent_verification.json').read_bytes()
        and seal['state'] == 'passed' and seal['manifest_sha256'] == digest(pack/'manifest.json')
        and completed_manifest['proposals_sha256'] == digest(pack/'proposals.jsonl'), 'Completed worklist seal differs')
    proposals = {p['target_key']: p for p in read_jsonl(pack/'proposals.jsonl')}
    require(len(proposals) == completed_manifest['request_count'], 'Completed worklist inventory differs')
    binding_events = {r['event_id']: r for r in read_jsonl(snapshot/'private/event_binding_audit.jsonl')}
    completed, raw_events = [], []
    with closing(readonly(snapshot/'private/gallery_snapshot.sqlite3', True)) as db:
        raw_events = list(db.execute('SELECT id,target_key,revision,payload FROM events ORDER BY id'))
        current = {k: (revision, raw) for k, revision, raw in db.execute('SELECT target_key,revision,payload FROM reviews')}
    histories = defaultdict(list)
    for event_id, key, revision, raw in raw_events:
        histories[key].append((event_id, revision, raw))
    for key, proposal in proposals.items():
        require(key in current, 'Reported completed image has no real save')
        revision, raw = current[key]; value = json.loads(raw)
        require(histories[key][-1][1:] == (revision, raw), 'Current is not the raw latest event')
        for event_id, _, event_raw in histories[key]:
            event = json.loads(event_raw); binding = binding_events[event_id]
            require(binding['namespace'] == pack.name and binding['event_specific_source_verified'] is True
                and event['base_proposal_id'] == proposal_sha(proposal)
                and event['proposal_publication'] == 15 and event['proposal_polygon'] == proposal['polygon']
                and event.get('human_edited') is True and event.get('event_kind') == 'human_mask_gallery_revision'
                and event['preannotation_provenance']['worklist_manifest_sha256'] == digest(pack/'manifest.json'),
                'Real save does not match its exact worklist/proposal/target source')
        completed.append(dict(target_key=key, class_id=proposal['class_id'], latest_revision=revision,
            saved_events=len(histories[key]), polygon_changed=value['polygon'] != proposal['polygon'],
            worklist_manifest_sha256=digest(pack/'manifest.json')))
    with closing(readonly(snapshot/'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_history = defaultdict(list)
        for key, raw in db.execute('SELECT target_key,payload FROM events ORDER BY id'):
            pose_history[key].append(json.loads(raw))
    protected = set(QUARANTINE) | {r['target_key'] for r in catalog}
    protected.update(p['target_key'] for p in read_jsonl(PARENT/'proposals.jsonl'))
    for key in set(pose_current) | set(pose_history):
        values = pose_history[key]+([pose_current[key]] if key in pose_current else [])
        if key not in pose_current or any(v.get('state') in ('deleted', 'deferred', 'confirmed') or confirmed(v)
                or not original_rectangle(v.get('polygon'), rows[key]) for v in values):
            protected.add(key)
    inventories = {}; historical_locations = defaultdict(list)
    for pattern in ('requests.jsonl', 'shortlist.jsonl'):
        for path in sorted((PROJECT/'outputs').rglob(pattern)):
            for item in read_jsonl(path):
                if item.get('target_key') in rows:
                    protected.add(item['target_key'])
                    historical_locations[item['target_key']].append(str(path.relative_to(PROJECT)))
            inventories[str(path.relative_to(PROJECT))] = digest(path)
    frames = {rows[key]['frame_id'] for key in protected}
    image_hash_cache = {}
    def image_digest(row):
        name = row['image']
        if name not in image_hash_cache:
            image_hash_cache[name] = digest(formal/name)
        return image_hash_cache[name]
    image_hashes = {image_digest(rows[key]) for key in protected}
    records = []; statuses = defaultdict(Counter); candidates = []
    saved_keys = {r['target_key'] for r in catalog}
    for key, row in sorted(rows.items()):
        if row['final_class_id'] not in gaps:
            continue
        status, quality = classify(row, key, protected, frames, image_hashes, evidence, image_digest)
        record = dict(target_key=key, class_id=row['final_class_id'], source=row, disposition=status,
            already_real_saved=key in saved_keys, historical_request_or_shortlist=historical_locations[key],
            source_image_sha256=image_digest(row), native_quality=quality)
        records.append(record); statuses[row['final_class_id']][status] += 1
        if status == 'new_native_quality_candidate_pending_visual_inspection':
            candidates.append(key)
    require(formal_before == protected_snapshot(formal), 'Formal protected files changed')
    require(file_inventory(PROJECT/'configs') == config_before, 'Global configs changed')
    for kind, path in (('pose', POSE), ('gallery', GALLERY)):
        require(fingerprint(path) == summary[kind+'_fingerprint'], 'Concurrent real save arrived; preserve audit input and repeat with a new snapshot')
    output.mkdir(mode=0o700); private = output/'private'; private.mkdir(mode=0o700)
    write(private/'remaining_formal_targets.jsonl', records, True)
    write(private/'completed_worklist_saves.jsonl', completed, True)
    result = dict(state='exhaustive_no_new_eligible_native_candidate' if not candidates else 'new_candidates_require_visual_preannotation',
        snapshot_directory=str(snapshot.relative_to(PROJECT)), source_audit_sha256=digest(snapshot/'audit.json'),
        actual_distinct_human_contours=len(catalog), actual_class_counts=dict(counts), remaining_gaps=gaps,
        gallery_records=summary['gallery_contours'], gallery_events=summary['gallery_events'],
        completed_worklist_directory=str(pack.relative_to(PROJECT)), completed_worklist_manifest_sha256=digest(pack/'manifest.json'),
        completed_worklist_distinct_images=len(completed), completed_worklist_saved_events=sum(r['saved_events'] for r in completed),
        completed_worklist_changed_polygons=sum(r['polygon_changed'] for r in completed),
        all_formal_remaining_class_targets=len(records), capacity_dispositions={c: dict(v) for c, v in statuses.items()},
        new_native_quality_candidate_keys=candidates, new_preannotations_issued=0, old_rejected_or_deferred_reissued=False,
        production_annotation_writes=0, global_configuration_writes=0, quality_gates_relaxed=False,
        frozen_training_append_or_retraining=False, formal_val_test_moved_to_train=False, contour_accuracy_certified=False,
        formal_manifest_sha256=manifest['formal_manifest_sha256'], formal_protected=formal_before,
        prior_worklist_inventories_sha256=inventories, code_sha256=digest(__file__),
        files={str(p.relative_to(output)): digest(p) for p in private.iterdir()}, audited_at=datetime.now(timezone.utc).isoformat())
    write(output/'audit.json', result)
    print(json.dumps({k: result[k] for k in ('state', 'actual_class_counts', 'remaining_gaps', 'completed_worklist_distinct_images',
        'completed_worklist_saved_events', 'all_formal_remaining_class_targets', 'capacity_dispositions', 'new_preannotations_issued')}), flush=True)
    return result


def proposal_sha(proposal):
    import hashlib
    return hashlib.sha256(canonical(proposal)).hexdigest()


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