"""Snapshot unit provenance, group scoring, and true continuation checks."""
import copy
import json
import os

import numpy as np
import pytest
import torch

from pallet.pose_benchmark_v4_data import effective_label
from pallet import pose_benchmark_v4 as benchmark


def label(key='confirmed',scale=None):
    return dict(target_key=key,branch='metric' if scale is not None else 'native_unknown',
                sensor=dict(scale_to_m=scale,axes_confirmed=False),
                pose=dict(x=2.,y=.1,theta_degrees=1.,position_unit='native_unknown',z=None,
                          robot_command_eligible=False))


def test_user_confirmation_scope_is_exact_and_source_unchanged():
    original=label();before=copy.deepcopy(original)
    result=effective_label(original,{'confirmed'})
    assert original==before
    assert result['branch']=='metric' and result['sensor']['scale_to_m']==1.
    assert result['source_sensor']['scale_to_m'] is None
    assert result['source_branch']=='native_unknown' and not result['sensor']['axes_confirmed']
    assert result['pose']['x']==2. and result['pose']['y']==.1 and result['pose']['z'] is None
    assert not result['robot_command_eligible']
    other=effective_label(label('unconfirmed'),{'confirmed'})
    assert other['branch']=='native_unknown' and other['sensor']['scale_to_m'] is None


def test_registered_or_explicit_sensor_scale_is_never_overridden():
    result=effective_label(label(scale=.001),{'confirmed'})
    assert result['sensor']['scale_to_m']==.001
    assert result['unit_evidence']=='source_registered_or_saved_human_policy'


def test_macro_group_score_counts_preprocessing_failures(monkeypatch):
    monkeypatch.setattr(benchmark,'config',lambda:dict(preprocessing_failure_score=100.))
    rows=[dict(target_key='a',group_id='g1'),dict(target_key='b',group_id='g2')]
    all_rows=rows+[dict(target_key='missing',group_id='g1')]
    y=np.array([[0,0,1,0],[0,0,1,0]],np.float32)
    result=benchmark.metrics(y,y,rows,all_rows)
    assert result['macro_group_selection_score']==25.
    assert result['total']==3 and result['failed_preprocessing']==1


def test_group_bootstrap_identical_predictions_have_zero_delta():
    rows=[dict(group_id='a'),dict(group_id='b')]
    y=np.array([[0,0,1,0],[0,0,1,0]],np.float32)
    interval=benchmark.interval([y,y],y,rows)
    assert interval['mean']==interval['lower']==interval['upper']==0.


def test_dataset_resume_keeps_exact_manifest_bytes(tmp_path,monkeypatch):
    from pallet import pose_benchmark_v4_data as data
    from pallet.cluster_targets import digest
    root=tmp_path;monkeypatch.setattr(data,'ROOT',root)
    monkeypatch.setattr(data,'snapshot',lambda:dict(frozen=True))
    monkeypatch.setattr(data,'config',lambda:{})
    (root/'snapshot').mkdir();(root/'dataset').mkdir()
    (root/'snapshot/manifest.json').write_text('{}')
    (root/'dataset/labels.jsonl').write_text('[]')
    (root/'dataset/features.jsonl').write_text('[]')
    frozen=dict(snapshot_sha256=digest(root/'snapshot/manifest.json'),created_at='freeze-once',
                labels_sha256=digest(root/'dataset/labels.jsonl'),feature_index_sha256=digest(root/'dataset/features.jsonl'))
    path=root/'dataset/manifest.json';path.write_text(json.dumps(frozen));before=path.read_bytes()
    assert data.prepare()==frozen and path.read_bytes()==before
    (root/'dataset/labels.jsonl').write_text('changed')
    with pytest.raises(ValueError,match='Frozen dataset index changed'):data.prepare()


def test_inference_checks_bundle_integrity_before_constructing_models(tmp_path):
    from pallet.pose_benchmark_v4_inference import V4PosePredictor
    (tmp_path/'protected.py').write_text('tampered')
    (tmp_path/'bundle_config.json').write_text(json.dumps(dict(schema='pose_benchmark_v4',files={'protected.py':'bad-hash'})))
    with pytest.raises(ValueError,match='Bundle hash mismatch'):V4PosePredictor(tmp_path,device='cpu')


def test_full_denominator_holdout_stats_and_mm_conversion():
    from pallet.pose_benchmark_v4_results import stats
    y=np.array([[0,0,1,0],[0,0,1,0]],np.float32)
    prediction=y.copy();prediction[:,0]=[.025,.1]
    rows=[dict(branch='metric',group_id='g',target_key=str(i)) for i in range(2)]
    all_rows=rows+[dict(branch='metric',group_id='g',target_key='failed')]
    value=stats(prediction,rows,dict(y=y,width=np.array([2.,1.])),all_rows)
    assert value['position_unit']=='mm'
    assert value['position_mean']==pytest.approx(75,abs=1e-5)
    # Small float32 errors at exactly 50mm are not silently widened away.
    assert value['predictions']==2 and value['failed_preprocessing']==1 and value['total']==3


def test_frozen_units_recomputed_and_train_folds_disjoint():
    from pallet.pose_benchmark_v4_data import ROOT
    if not (ROOT/'protocol.json').exists():pytest.skip('Optional local benchmark outputs')
    rows=[json.loads(line) for line in (ROOT/'snapshot/eligible.jsonl').read_text().splitlines()]
    bykey={r['target_key']:r for r in rows}
    features={r['target_key']:r for r in map(json.loads,(ROOT/'dataset/features.jsonl').read_text().splitlines())}
    converted=[r for r in rows if r['source_branch']!=r['branch']]
    assert len(converted)==823
    assert all(not features[r['target_key']]['reused_v3_numerical_inputs'] for r in converted)
    protocol=json.loads((ROOT/'protocol.json').read_text())
    for branch,spec in protocol['branches'].items():
        seen=[]
        for fold in spec['folds']:
            ti,vi=fold['fit_keys'],fold['validation_keys']
            assert all(bykey[k]['split']=='train' and bykey[k]['branch']==branch for k in ti+vi)
            assert not ({bykey[k]['group_id'] for k in ti}&{bykey[k]['group_id'] for k in vi})
            seen.extend(vi)
        assert len(seen)==len(set(seen))==spec['train_samples']


@pytest.mark.skipif(os.environ.get('RUN_V4_GPU_TEST')!='1',reason='Explicit GPU continuation equivalence smoke test')
def test_optimizer_rng_resume_matches_uninterrupted_training(tmp_path,monkeypatch):
    monkeypatch.setattr(benchmark,'ROOT',tmp_path)
    (tmp_path/'protocol.json').write_text('{}')
    cfg=dict(base_batch=4,base_epochs=6,evaluation_batch=8,head_learning_rate=.001,
             backbone_learning_rate=.0001,weight_decay=.005,preprocessing_failure_score=100.,
             continuation=[dict(name='same_batch',batch=4,total_epochs=6)])
    monkeypatch.setattr(benchmark,'config',lambda:cfg)
    rng=np.random.default_rng(4)
    x=rng.normal(size=(12,3,5)).astype('float32')
    arrays={'y':np.tile([.01,-.02,1.,0.],(12,1)).astype('float32')}
    rows=[dict(target_key=str(i),group_id='fit' if i<8 else 'validation',split='train') for i in range(12)]
    ti,vi=list(range(8)),list(range(8,12))
    benchmark.train('utonia','metric',42,arrays,rows,rows,ti,vi,tmp_path/'uninterrupted',x=x)
    cfg['base_epochs']=3
    benchmark.train('utonia','metric',42,arrays,rows,rows,ti,vi,tmp_path/'parent',x=x)
    benchmark.train('utonia','metric',42,arrays,rows,rows,ti,vi,tmp_path/'continued',x=x,
                    arm='same_batch',resume=tmp_path/'parent')
    full=torch.load(tmp_path/'uninterrupted/last.pt',map_location='cpu',weights_only=False)
    resumed=torch.load(tmp_path/'continued/last.pt',map_location='cpu',weights_only=False)
    assert full['optimizer_steps']==resumed['optimizer_steps']==12
    assert full['numpy_rng']==resumed['numpy_rng']
    torch.testing.assert_close(full['torch_rng'],resumed['torch_rng'],atol=0,rtol=0)
    torch.testing.assert_close(full['cuda_rng'],resumed['cuda_rng'],atol=0,rtol=0)
    for key in full['model']:
        torch.testing.assert_close(full['model'][key],resumed['model'][key],atol=0,rtol=0)
    assert full['history']==resumed['history']
    binding=json.loads((tmp_path/'continued/continuation_binding.json').read_text())
    assert binding['optimizer_and_rng_restored'] and binding['start_epoch']==3
