"""Read-only remaining-space survey. No delete, relink, model load or training.

Writes a fresh evidence directory only. Capacity proposals are not permissions
and are not all immediately safe or additive.
"""
from __future__ import annotations

from collections import Counter, defaultdict
from datetime import datetime, timezone
import hashlib
import json
import os
from pathlib import Path
import shutil
import subprocess
import zipfile

PROJECT = Path(__file__).resolve().parents[1]
OUTPUT = PROJECT/'outputs/pallet_storage_reclaim_analysis_v1'


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


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


def relative(path):
    return str(Path(path).relative_to(PROJECT))


def files(root):
    return sorted(p for p in Path(root).rglob('*') if p.is_file() and not p.is_symlink())


def capacity(paths):
    paths = list(paths); allocated = 0; inodes = set(); linked = 0
    for path in paths:
        st = path.stat(); identity = (st.st_dev, st.st_ino)
        if st.st_nlink > 1:
            linked += 1
        if identity not in inodes:
            allocated += st.st_blocks*512; inodes.add(identity)
    return dict(files=len(paths), allocated_bytes=allocated, gib=allocated/2**30,
        hardlinked_files_needing_separate_review=linked,
        allocation_not_a_verified_net_reclaim_guarantee=True)


def analyze(out=OUTPUT):
    out = Path(out).absolute()
    if out.parent != PROJECT/'outputs' or out.exists():
        raise ValueError('Write a fresh top-level outputs analysis directory')
    hash_cache = {}
    def digest(path):
        path = Path(path); st = path.stat(); key = (str(path), st.st_size, st.st_mtime_ns, st.st_ino)
        if key not in hash_cache:
            with path.open('rb') as stream:
                hash_cache[key] = hashlib.file_digest(stream, 'sha256').hexdigest()
            after = path.stat()
            if (st.st_size, st.st_mtime_ns, st.st_ino) != (after.st_size, after.st_mtime_ns, after.st_ino):
                raise ValueError('File changed during read-only hashing: '+str(path))
        return hash_cache[key]
    active = {relative(p): digest(p) for p in (PROJECT/'configs').glob('active*.json')}
    usage = subprocess.run(['du', '-x', '-B1', '--max-depth=1', str(PROJECT/'outputs')],
        capture_output=True, text=True, check=True)
    by_root = {}; total = 0
    for line in usage.stdout.splitlines():
        size, path = line.split('\t', 1)
        if Path(path) == PROJECT/'outputs':
            total = int(size)
        else:
            by_root[relative(path)] = int(size)
    result = dict(schema='remaining_output_space_analysis_v1', state='analysis_only_no_deletion',
        measured_at=datetime.now(timezone.utc).isoformat(), output_allocated_bytes=total,
        free_bytes_at_start=shutil.disk_usage(PROJECT/'outputs').free,
        root_allocated_bytes=by_root, active_configuration_sha256=active,
        new_work_not_classified_as_obsolete=[relative(PROJECT/'outputs'/name)
            for name in ('pallet_mask_model_training_v1', 'pallet_mask_under50_requests_v1')
            if (PROJECT/'outputs'/name).exists()], groups={})
    groups = result['groups']
    # Exact-copy construction payload: canonical ZIP and original deliveries stay.
    original = PROJECT/'outputs/pallet_dataset_formal_v1/pallet_dataset_formal_v1.zip'
    payload = PROJECT/'outputs/baselines_8x4090_handoff_v1/pallet_baselines_8x4090_handoff_v1/payload/pallet_dataset_formal_v1.zip'
    same = original.stat().st_size == payload.stat().st_size and digest(original) == digest(payload)
    groups['handoff_duplicate_payload'] = dict(**capacity([payload]), exact_sha256_duplicate_verified=same,
        candidate=relative(payload), retained_original=relative(original), sha256=digest(original),
        effect='unpacked handoff verification requires restoring this payload copy; keep both original delivery ZIPs',
        approval_required=True)
    print(json.dumps(dict(stage='handoff_payload_verified', exact=same)), flush=True)
    # Download archive is redundant only if every extracted member still agrees.
    tooling = PROJECT/'outputs/fork_face_browser_tooling'; archive = tooling/'chrome-linux64.zip'; members = 0
    chrome_ok = True
    with zipfile.ZipFile(archive) as zipped:
        for member in zipped.infolist():
            if member.is_dir():
                continue
            extracted = tooling/member.filename
            if '..' in Path(member.filename).parts or not extracted.is_file() or extracted.is_symlink():
                chrome_ok = False; break
            with zipped.open(member) as stream:
                stored = hashlib.file_digest(stream, 'sha256').hexdigest()
            if stored != digest(extracted):
                chrome_ok = False; break
            members += 1
    groups['browser_download_archive'] = dict(**capacity([archive]), candidate=relative(archive),
        every_extracted_member_byte_verified=chrome_ok, verified_members=members,
        keep_executable_font_and_license=True, approval_required=True)
    test_roots = [PROJECT/'outputs'/n for n in ('fork_face_test_tmp', 'fork_face_regression_tmp', 'test_8x4090_handoff_tmp')]
    test_files = [p for root in test_roots for p in files(root)]
    groups['pytest_synthetic_fixtures'] = dict(**capacity(test_files),
        roots=[relative(p) for p in test_roots], observed_synthetic_test_directory_layout=True,
        active_file_handles_and_external_references_not_exhaustively_checked=True, approval_required=True)
    # Old segmentation run: retain both archived, actually selected model files.
    seg = PROJECT/'outputs/pallet_instance_segmentation_seed_v1'; keep = []
    for kind in ('small', 'medium'):
        completion = read(seg/'training'/kind/'completion.json')
        weight = seg/'training'/kind/completion['checkpoint']
        if completion['state'] != 'complete' or digest(weight) != completion['checkpoint_sha256']:
            raise ValueError('Old segmentation best model identity failed')
        keep.append(weight)
    seg_models = [p for p in files(seg/'training') if p.suffix in ('.pth', '.ckpt', '.pt')]
    redundant = [p for p in seg_models if p not in keep]
    groups['old_segmentation_nonbest_checkpoints'] = dict(**capacity(redundant),
        candidate_paths=[relative(p) for p in redundant], best_models_to_keep=[relative(p) for p in keep],
        selected_best_hashes_verified=True, candidate_bytes_not_all_rehashed=True,
        effect='lose old Lightning optimizer/RNG restart; preserve logs, dataset, two best files and delivery ZIPs',
        old_finetuned_weights_are_not_clean_initialization_for_new_val_test=True, approval_required=True)
    smoke_roots = [PROJECT/'outputs'/name for name in ('recipe_cli_smoke_v5', 'recipe_rf_cli_smoke_v5_gpu1', 'recipe_rf_pretrained_smoke_v5')]
    smoke = [p for root in smoke_roots for p in files(root) if p.suffix == '.pt']
    groups['old_recipe_smoke_weights'] = dict(**capacity(smoke), candidate_paths=[relative(p) for p in smoke],
        best_only_alternative_reclaim=capacity(p for p in smoke if p.name != 'best.pt'),
        no_direct_active_configuration_reference=not any(relative(p) in (PROJECT/c).read_text() for c in active for p in smoke),
        historical_reference_and_open_file_checks_still_required=True, keep_logs_and_metrics=True, approval_required=True)
    v3 = PROJECT/'outputs/pallet_pose_benchmark_v3'
    v3_trials = list((v3/'trials').rglob('selected.pt'))
    groups['v3_fold_weight_upper_bound'] = dict(**capacity(v3_trials),
        candidate_paths=[relative(p) for p in v3_trials], completed_markers=sum((p.parent/'completed.json').exists() for p in v3_trials),
        refitted_models_currently_present=[relative(p) for p in (v3/'refit').rglob('selected.pt')],
        not_safe_to_drop_all_if_preserving_all_eight_model_warmstarts=True,
        explanation='v3 refit retains PointNeXt winners only; keep additional architecture-specific fold warmstarts before pruning',
        entire_v3_root_cannot_be_removed_because_v4_v5_use_official_assets=True, approval_required=True)
    detector_roots = [PROJECT/'outputs'/n for n in ('pilot600_v5', 'rounds_v5', 'learning_120_v4', 'paired_learning_60_v5')]
    detector_roots += [p for p in (PROJECT/'outputs').glob('human_correction*') if p.is_dir()]
    detector_candidates = []; per_root = {}
    for root in sorted(set(detector_roots)):
        candidates = [p for p in files(root) if p.name in ('last.pt', 'best.previous.pt')
            and not {'deployment', 'snapshot', 'delivery', 'inputs'} & set(p.relative_to(root).parts)]
        detector_candidates.extend(candidates); per_root[relative(root)] = capacity(candidates)
    groups['other_old_detector_last_and_previous'] = dict(**capacity(detector_candidates),
        by_root=per_root, candidate_paths=[relative(p) for p in detector_candidates],
        best_pt_and_all_delivery_archives_excluded=True, live_training_and_best_identity_checks_pending=True,
        effect='optimizer restart and historical previous-best replay may be lost; does not include original review data',
        approval_required=True)
    print(json.dumps(dict(stage='checkpoint_capacities_measured')), flush=True)
    # Compare exact input copies; this is NOT approval to delete source materials.
    canonical_inputs = PROJECT/'outputs/pallet_pose_dataset_v2/inputs'; raw_groups = []
    for name in ('pallet_pose_dataset_interrupted_v1', 'pallet_pose_dataset_v1'):
        root = PROJECT/'outputs'/name/'inputs'; matches = []; differences = []
        for path in files(root):
            other = canonical_inputs/path.relative_to(root)
            if other.is_file() and path.stat().st_size == other.stat().st_size and digest(path) == digest(other):
                matches.append(path)
            else:
                differences.append(relative(path))
        raw_groups.append(dict(root=relative(root), **capacity(matches), exact_matches=[relative(p) for p in matches],
            unmatched=differences, byte_hashes_fully_verified=True))
        print(json.dumps(dict(stage='input_copies_verified', root=name, matches=len(matches), unmatched=len(differences))), flush=True)
    interrupted = PROJECT/'outputs/pallet_pose_dataset_interrupted_v1'; malformed = []
    for path in (interrupted/'dataset').rglob('*.npz'):
        try:
            with zipfile.ZipFile(path) as zipped:
                bad_member = zipped.testzip()
                if bad_member:
                    malformed.append(dict(path=relative(path), reason='CRC failure: '+bad_member, bytes=path.stat().st_size))
        except zipfile.BadZipFile as exc:
            malformed.append(dict(path=relative(path), reason=str(exc), bytes=path.stat().st_size))
    groups['raw_input_copy_dedup_only'] = dict(groups=raw_groups,
        exact_duplicate_allocated_bytes=sum(g['allocated_bytes'] for g in raw_groups),
        source_material_not_ordinary_disposable_cache=True,
        require_preserved_original_paths_and_readonly_source_dedup_or_explicit_archive_plan=True,
        not_in_immediate_deletion_total=True)
    groups['interrupted_build'] = dict(**capacity(files(interrupted)), root=relative(interrupted),
        malformed_npz=malformed, keep_fault_evidence_and_input_hash_manifest=True,
        overlaps_input_copy_capacity=True, not_in_additive_reclaim_total=True)
    # Preview versus exact-measurement data is an important safety boundary.
    geometry = {}
    for name in ('pallet_pose_refinement_v2/cache/automatic_geometry', 'pallet_pose_human_review_v1/geometry'):
        root = PROJECT/'outputs'/name
        geometry[name] = {suffix: capacity(root.glob('*'+suffix)) for suffix in ('.bin', '.npz', '.json')}
    groups['geometry_preview_candidates_not_yet_qualified'] = dict(roots=geometry,
        offline_preview_potential_bytes=geometry['pallet_pose_refinement_v2/cache/automatic_geometry']['.bin']['allocated_bytes'],
        live_preview_potential_bytes=geometry['pallet_pose_human_review_v1/geometry']['.bin']['allocated_bytes'],
        keep_all_npz_json_and_human_artifacts=True, not_an_immediate_deletion_recommendation=True,
        code_hazard='GeometryEngine.compute recomputes and overwrites npz/json when bin is missing; requires non-overwriting recovery design first',
        no_regeneration_byte_equivalence_audit_performed=True)
    parents = {}
    for root in (PROJECT/'outputs').glob('pallet_mask_remaining*'):
        if (root/'parent_evidence').is_dir():
            parents[relative(root/'parent_evidence')] = capacity(files(root/'parent_evidence'))
    groups['sealed_parent_evidence_not_disposable'] = dict(copies=parents,
        gross_allocated_bytes=sum(v['allocated_bytes'] for v in parents.values()),
        direct_deletion_would_break_seals_or_history=True, requires_separate_immutable_dedup_design=True,
        not_in_immediate_deletion_total=True)
    # Active v7 and the prior cleanup recovery plan both retain dependencies on v5.
    active_index = read(PROJECT/'outputs/cache_v7_oriented/sealed_index.json')
    v5_refs = {v['path'] for v in active_index.values() if '/outputs/cache_v5/frames/' in v['path']}
    prior_plan = read(PROJECT/'outputs/pallet_review_intermediate_cleanup_v1/plan.json')
    restoration = {c['source'] for c in prior_plan['candidates'] if c['source'].startswith('outputs/cache_v5/frames/')}
    groups['master_cache_do_not_delete'] = dict(root='outputs/cache_v5', allocated_bytes=by_root['outputs/cache_v5'],
        active_v7_master_cache_paths=len(v5_refs), prior_review_cleanup_master_restoration_sources=len(restoration),
        current_or_restoration_dependencies_present=True)
    if active != {relative(p): digest(p) for p in (PROJECT/'configs').glob('active*.json')}:
        raise ValueError('Active configuration changed during survey; re-evaluate against fresh references')
    result.update(active_configurations_unchanged_during_survey=True,
        existing_files_deleted=0, existing_data_or_configuration_writes=0, training_or_model_initialization_launched=False,
        low_risk_recoverable_group_names=['handoff_duplicate_payload', 'browser_download_archive', 'pytest_synthetic_fixtures'],
        independently_gated_model_group_names=['old_segmentation_nonbest_checkpoints', 'old_recipe_smoke_weights',
            'v3_fold_weight_upper_bound', 'other_old_detector_last_and_previous'],
        all_group_totals_must_not_be_blindly_added=True, free_bytes_at_end=shutil.disk_usage(PROJECT/'outputs').free,
        script_sha256=digest(__file__))
    out.mkdir(mode=0o700)
    with (out/'analysis.json').open('xb') as stream:
        stream.write(canonical(result)+b'\n')
    print(json.dumps(dict(state=result['state'], output=relative(out/'analysis.json'), existing_files_deleted=0,
        low_risk_gib=sum(groups[g]['allocated_bytes'] for g in result['low_risk_recoverable_group_names'])/2**30,
        old_segmentation_nonbest_gib=groups['old_segmentation_nonbest_checkpoints']['gib'],
        smoke_weight_gib=groups['old_recipe_smoke_weights']['gib'],
        v3_fold_weight_upper_bound_gib=groups['v3_fold_weight_upper_bound']['gib'],
        old_detector_last_previous_gib=groups['other_old_detector_last_and_previous']['gib'])), flush=True)
    return result


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--output', type=Path, default=OUTPUT)
    analyze(parser.parse_args().output)
