"""Best-refit checkpoint pruning never touches live data or published packages."""
import importlib.util
import json
from pathlib import Path
import sqlite3

import pytest
import torch

SCRIPT = Path(__file__).resolve().parents[1]/'scripts/compact_pose_v4_checkpoints.py'
SPEC = importlib.util.spec_from_file_location('compact_pose_v4_checkpoints', SCRIPT)
compact = importlib.util.module_from_spec(SPEC); SPEC.loader.exec_module(compact)


@pytest.mark.parametrize('path', [
    'outputs/pallet_pose_benchmark_v4/deployment/models/ptv3/metric/base.pt',
    'outputs/pallet_pose_benchmark_v4/pallet_pose_models_v4.zip',
    'outputs/pallet_pose_benchmark_v4/snapshot/human.pt',
    'outputs/pallet_pose_benchmark_v4/trials/base/ptv3/metric/42/fold_0/history.json',
    'outputs/pallet_pose_benchmark_v4/refit/ptv3/metric/base/weights.pt',
    'outputs/pallet_pose_benchmark_v4/refit/ptv3/metric/base/epochs/0.pt',
    'outputs/pallet_pose_benchmark_v3/refit/ptv3/metric/base/last.pt',
    'outputs/pallet_pose_human_review_v1/last.pt',
])
def test_nontraining_or_unknown_paths_are_never_deletion_eligible(path):
    assert not compact.eligible(path, ['ptv3'], ['base'])


def test_only_explicit_layouts_match():
    assert compact.eligible('outputs/pallet_pose_benchmark_v4/refit/ptv3/metric/base/last.pt', ['ptv3'], ['base'])
    assert compact.eligible('outputs/pallet_pose_benchmark_v4/refit/ptv3/metric/base/epochs/20.pt', ['ptv3'], ['base'])
    assert compact.eligible('outputs/pallet_pose_benchmark_v4/trials/base/ptv3/metric/42/fold_0/selected.pt', ['ptv3'], ['base'])


def save_json(path, value):
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps(value))


@pytest.fixture
def environment(tmp_path, monkeypatch):
    monkeypatch.setattr(compact, 'PROJECT', tmp_path)
    root = tmp_path/compact.ROOT_NAME; root.mkdir(parents=True)
    (root/'pipeline.lock').touch(); (root/'protocol.json').write_text('{}')
    config = dict(families=['ptv3', 'dgcnn'], continuation=[dict(name='long_b32')])
    save_json(tmp_path/compact.CONFIG_NAME, config)
    for name in compact.helpers().STORES:
        path = tmp_path/name; path.parent.mkdir(parents=True, exist_ok=True)
        with sqlite3.connect(path) as db:
            db.execute('CREATE TABLE events(id INTEGER,payload TEXT)')
            db.execute('INSERT INTO events VALUES(1,?)', ('真实人工事件',))
    formal = tmp_path/'outputs/pallet_dataset_formal_v1/dataset'; formal.mkdir(parents=True)
    (formal/'manifest.json').write_text('{"unchanged":true}')
    bundle = dict(schema='pose_benchmark_v4', files={}, model_catalog={})
    selection = dict(val_test_used=False, profiles={branch: dict(entries=[]) for branch in compact.BRANCHES})
    for family in config['families']:
        bundle['model_catalog'][family] = {}
        for branch in compact.BRANCHES:
            bundle['model_catalog'][family][branch] = {}; records = []
            for arm in ('base', 'long_b32'):
                score = 1 if (family == 'ptv3') == (arm == 'long_b32') else 2
                entry = dict(family=family, arm=arm, mean_score=score, refit_epoch=1)
                selection['profiles'][branch]['entries'].append(entry)
                folder = root/'refit'/family/branch/arm
                epoch = folder/'epochs/1.pt'; epoch.parent.mkdir(parents=True)
                state = dict(model={'weight': torch.tensor([1., 2.])}, family=family, selected_epoch=1, frozen_encoder=False, tune=False)
                torch.save(state, epoch)
                sha = compact.digest(epoch)
                records.append(dict(**entry, checkpoint=str(epoch.relative_to(root)), checkpoint_sha256=sha,
                    train_samples=12, train_groups=3, training_domains=['sensor']))
                deploy = root/'deployment/models'/family/branch/(arm+'.pt'); deploy.parent.mkdir(parents=True, exist_ok=True)
                deploy.write_bytes(epoch.read_bytes()); deploy_key = str(deploy.relative_to(root/'deployment'))
                bundle['files'][deploy_key] = sha
                bundle['model_catalog'][family][branch][arm] = dict(checkpoint=deploy_key, refit_epoch=1)
                for run in (folder, root/'trials'/arm/family/branch/'42/fold_0'):
                    run.mkdir(parents=True, exist_ok=True)
                    (run/'last.pt').write_bytes(b'optimizer and RNG state')
                    torch.save(dict(state, selected_epoch=2), run/'selected.pt')
                    spec = dict(total_epochs=2, family=family, branch=branch, arm=arm, validation_keys=[],
                        fit_keys=['train-1'], protocol_sha256=compact.digest(root/'protocol.json'))
                    save_json(run/'specification.json', spec)
                    save_json(run/'completed.json', dict(last_epoch=2, selected_epoch=2,
                        last_sha256=compact.digest(run/'last.pt'), selected_sha256=compact.digest(run/'selected.pt')))
                    (run/'history.json').write_text('[{"epoch":1},{"epoch":2}]')
            save_json(root/'refit'/family/branch/'profiles.json', records)
    save_json(root/'selection_frozen.json', selection)
    save_json(root/'deployment/bundle_config.json', bundle)
    delivery = {}
    for name in ('models', 'annotations'):
        path = root/f'pallet_pose_{name}_v4.zip'; path.write_bytes(b'keep original published ZIP')
        delivery[name] = dict(archive=compact.relative(path), sha256=compact.digest(path))
    save_json(root/'delivery_manifest.json', delivery)
    return tmp_path, root


def test_plan_is_readonly_apply_keeps_best_refits_and_whole_published_bundle(environment):
    project, root = environment
    before = {str(p): p.read_bytes() for p in root.rglob('*') if p.is_file()}
    out = project/'outputs/compact_test'; plan = compact.plan(out)
    assert len(plan['retained_best_models']) == 4
    assert all(Path(p).read_bytes() == v for p, v in before.items())
    assert plan['counts'] == {'last.pt': 16, 'selected.pt': 16, 'nonwinning_refit_epoch.pt': 4}
    kept = {m['checkpoint'] for m in plan['retained_best_models']}
    assert all(('long_b32' in m['checkpoint']) == (m['family'] == 'ptv3') for m in plan['retained_best_models'])
    result = compact.apply(out, compact.digest(out/'plan.json'))
    assert result['deleted_files'] == 36 and result['retained_best_training_weights'] == 4
    assert not result['exact_optimizer_rng_resume_available']
    assert {compact.relative(p) for tree in ('trials', 'refit') for p in (root/tree).rglob('*.pt')} == kept
    deleted = {str(project/c['path']) for c in plan['candidates']}
    assert all(Path(p).read_bytes() == v for p, v in before.items() if p not in deleted)
    assert result['deployment_and_archives_byte_unchanged']


def test_holdout_based_selection_is_rejected():
    with pytest.raises(ValueError, match='val/test'):
        compact.best_entries(dict(val_test_used=True), ['ptv3'])


def test_exact_cv_tie_uses_original_frozen_order_not_latest_recipe():
    profile = dict(entries=[dict(family='ptv3', arm=arm, mean_score=1., refit_epoch=3) for arm in ('base', 'long_b32')])
    choices = compact.best_entries(dict(val_test_used=False, profiles={b: profile for b in compact.BRANCHES}), ['ptv3'])
    assert [m['arm'] for m in choices] == ['base', 'base']


def test_corrupted_best_weight_blocks_planning_before_deletion(environment):
    project, root = environment
    path = root/'refit/ptv3/metric/long_b32/epochs/1.pt'; path.write_bytes(b'corrupt')
    with pytest.raises(ValueError, match='Best refit bytes'):
        compact.plan(project/'outputs/compact_test')
    assert next((root/'trials').rglob('last.pt')).exists()


def test_changed_checkpoint_blocks_entire_apply(environment):
    project, root = environment
    out = project/'outputs/compact_test'; plan = compact.plan(out)
    target = project/plan['candidates'][0]['path']; target.write_bytes(b'new state')
    with pytest.raises(ValueError, match='opened or changed'):
        compact.apply(out, compact.digest(out/'plan.json'))
    assert all((project/c['path']).exists() for c in plan['candidates'])
    assert not (out/'deletion_journal.jsonl').exists()


def test_changed_human_database_blocks_entire_apply(environment):
    project, root = environment
    out = project/'outputs/compact_test'; plan = compact.plan(out)
    path = project/compact.helpers().STORES[0]
    with sqlite3.connect(path) as db:
        db.execute('INSERT INTO events VALUES(2,?)', ('新的人工保存',))
    with pytest.raises(ValueError, match='Protected sources changed'):
        compact.apply(out, compact.digest(out/'plan.json'))
    assert all((project/c['path']).exists() for c in plan['candidates'])


def test_wrong_plan_hash_does_not_delete(environment):
    project, root = environment
    out = project/'outputs/compact_test'; plan = compact.plan(out)
    with pytest.raises(ValueError, match='plan hash'):
        compact.apply(out, '0'*64)
    assert all((project/c['path']).exists() for c in plan['candidates'])


def test_nonfinite_best_state_is_rejected(tmp_path):
    path = tmp_path/'bad.pt'
    torch.save(dict(family='ptv3', selected_epoch=1, frozen_encoder=False, tune=False,
        model={'weight': torch.tensor([float('nan')])}), path)
    with pytest.raises(ValueError, match='Nonfinite'):
        compact.validate_weight(path, 'ptv3', 1)


def test_unknown_new_checkpoint_is_never_silently_deleted(environment):
    project, root = environment
    path = root/'refit/ptv3/metric/base/unknown.pt'; path.write_bytes(b'potential user work')
    with pytest.raises(ValueError, match='Unexpected training weight'):
        compact.plan(project/'outputs/compact_test')
    assert path.exists()


def test_running_pipeline_lock_blocks_planning(environment):
    import fcntl
    project, root = environment
    with (root/'pipeline.lock').open('rb') as stream:
        fcntl.flock(stream, fcntl.LOCK_EX | fcntl.LOCK_NB)
        with pytest.raises(ValueError, match='pipeline is active'):
            compact.plan(project/'outputs/compact_test')
    assert next((root/'trials').rglob('last.pt')).exists()


def test_hardlinked_training_checkpoint_is_preserved(environment):
    import os
    project, root = environment
    path = next((root/'trials').rglob('last.pt'))
    os.link(path, project/'keep_external_copy.pt')
    with pytest.raises(ValueError, match='hardlinked'):
        compact.plan(project/'outputs/compact_test')
    assert path.exists()


def test_symlink_to_best_checkpoint_is_rejected(environment):
    project, root = environment
    path = root/'refit/ptv3/metric/long_b32/epochs/1.pt'
    destination = project/'external_best.pt'; path.rename(destination); path.symlink_to(destination)
    with pytest.raises(ValueError, match='Symlink'):
        compact.plan(project/'outputs/compact_test')
    assert destination.exists()


def test_existing_audit_directory_cannot_be_overwritten(environment):
    project, root = environment
    out = project/'outputs/compact_test'; out.mkdir()
    (out/'user_notes.md').write_text('preserve')
    with pytest.raises(ValueError, match='overwrite'):
        compact.plan(out)
    assert (out/'user_notes.md').read_text() == 'preserve'
