from copy import deepcopy
import hashlib
import json
from pathlib import Path
import zipfile

import numpy as np
from PIL import Image
import pytest

from scripts.create_8x4090_handoff import source_files, verify_outer_archive
from scripts.verify_8x4090_handoff import (digest, safe_path, validate_config,
                                         verify_frozen_files, verify_portable_dataset,
                                         unpack_dataset, dataset_zip_entries)


ROOT = Path(__file__).resolve().parents[1]


def config():
    return json.loads((ROOT/'configs/baselines_8x4090_handoff.json').read_text())


def freeze(root, summary):
    files = {str(p.relative_to(root)): digest(p) for p in root.rglob('*')
             if p.is_file() and p.name not in {'manifest.json','checksums.sha256'}}
    (root/'manifest.json').write_text(json.dumps({'files': files, 'summary': summary}))
    hashes = {**files, 'manifest.json': digest(root/'manifest.json')}
    (root/'checksums.sha256').write_text(''.join(f'{sha}  {name}\n' for name,sha in hashes.items()))


def fixture_dataset(tmp_path):
    root = tmp_path/'data'
    root.mkdir()
    rows, annotations, tensor_index = [], [], {}
    splits = {'assignments': {}, 'frame_ids': {s:[] for s in ('train','val','test')}}
    for index, split in enumerate(('train','val','test')):
        fid, group = f'frame{index}', f'group{index}'
        w,h = 8,6
        boxes = [] if split == 'val' else [{'x':1.,'y':2.,'width':3.,'height':2.}]
        paths = {'image':f'images/{split}/{fid}.png', 'annotation':f'images/{split}/{fid}.json',
                 'yolo_label':f'labels/{split}/{fid}.txt', 'sensor':f'sensors/{split}/{fid}.npz',
                 'depth':f'auxiliary/{split}/{fid}.depth.png',
                 'depth_validity':f'auxiliary/{split}/{fid}.depth_valid.png',
                 'intensity_validity':f'auxiliary/{split}/{fid}.intensity_valid.png'}
        for name in paths.values(): (root/name).parent.mkdir(parents=True,exist_ok=True)
        data = np.zeros((h,w,4),np.float32)
        data[...,1]=.5;data[...,3]=1
        if split == 'train': data[...,0]=100;data[...,2]=1
        source = hashlib.sha256(fid.encode()).hexdigest()
        metadata = {'source_sha256': source}
        np.savez(root/paths['sensor'],frame=data,metadata=json.dumps(metadata))
        for key, pixels in [('image',np.full((h,w),128,np.uint8)), ('depth',np.zeros((h,w),np.uint8)),
                            ('depth_validity',data[...,2]*255),('intensity_validity',data[...,3]*255)]:
            Image.fromarray(pixels.astype(np.uint8)).save(root/paths[key])
        status = 'pos' if boxes else 'negative'
        doc = {'imageWidth':w,'imageHeight':h,'imagePath':fid+'.png',
               'flags':{'human_reviewed':True,'confirmed_negative':not boxes,'proposal_only':False,'unreviewed':False},
               'shapes':[{'label':'pallet','shape_type':'rectangle','points':[[1.,2.],[4.,4.]]} for _ in boxes]}
        (root/paths['annotation']).write_text(json.dumps(doc))
        (root/paths['yolo_label']).write_text('0 0.3125 0.5 0.375 0.3333333333333333\n' if boxes else '')
        row = {**paths,'frame_id':fid,'group_id':group,'split':split,'status':status,
               'human_reviewed':True,'proposal_only':False,'independent_blind_reference':False,
               'source_anchor_hash':source,'domain':'example','depth_available':split=='train',
               'pixel_rotation_degrees':0,'reconstructed_intensity':False}
        annotation = {'frame_id':fid,'group_id':group,'split':split,'status':status,'width':w,'height':h,
                      'boxes':boxes,'provenance':[{'source':'human'}]}
        rows.append(row);annotations.append(annotation)
        splits['assignments'][group]=split;splits['frame_ids'][split]=[fid]
        tensor_index[fid]={'path':paths['sensor']}
        (root/'annotations').mkdir(exist_ok=True)
        coco = {'categories':[{'id':1,'name':'pallet'}], 'images':[{'id':1,'frame_id':fid,'file_name':paths['image'],
                'width':w,'height':h,'split':split}], 'annotations':[
                    {'id':1,'image_id':1,'category_id':1,'iscrowd':0,'bbox':[1.,2.,3.,2.]} for _ in boxes]}
        (root/f'annotations/{split}.coco.json').write_text(json.dumps(coco))
    (root/'frames.jsonl').write_text(''.join(json.dumps(x)+'\n' for x in rows))
    (root/'annotations/frames.jsonl').write_text(''.join(json.dumps(x)+'\n' for x in annotations))
    (root/'splits.json').write_text(json.dumps(splits))
    (root/'tensor_index.json').write_text(json.dumps(tensor_index))
    counts = {'train':1,'val':1,'test':1}
    freeze(root,{'frames':3,'boxes':2,'groups':3,'split_counts':counts})
    cfg = config()
    cfg['dataset'].update(frames=3,boxes=2,groups=3,positives=2,negatives=1,splits=counts,
                          manifest_sha256=digest(root/'manifest.json'),
                          annotations_sha256=digest(root/'annotations/frames.jsonl'))
    return root,cfg


def test_pending_eight_runs_are_frozen_and_no_distributed_batch_growth():
    value = config()
    assert len(validate_config(value)) == 8
    assert value['scheduling']['distributed_training'] is False
    bad = deepcopy(value);bad['experiments'][0]['execution_status']='complete'
    with pytest.raises(ValueError,match='pending'): validate_config(bad)
    bad = deepcopy(value);bad['training']['effective_batch']=128
    with pytest.raises(ValueError,match='protocol'): validate_config(bad)
    bad = deepcopy(value);bad['dataset']['independent_blind_reference']=True
    with pytest.raises(ValueError,match='blind'): validate_config(bad)


@pytest.mark.parametrize('path',['../outside','/root/x','C:/x','a/../../b','a\\b','.',''])
def test_unsafe_portable_paths_are_rejected(tmp_path,path):
    with pytest.raises(ValueError): safe_path(tmp_path,path)


def test_snapshot_captures_untracked_code_and_excludes_history(tmp_path):
    for name in ['pallet/new.py','tests/test_new.py','configs/new.json','scripts/new.sh',
                 'pallet/__pycache__/a.py','outputs/old.py','.venv/old.py']:
        p=tmp_path/name;p.parent.mkdir(parents=True,exist_ok=True);p.write_text('test')
    names={str(p.relative_to(tmp_path)) for p in source_files(tmp_path)}
    assert names == {'pallet/new.py','tests/test_new.py','configs/new.json','scripts/new.sh'}


def test_portable_all_formats_negative_and_npz_without_original_library(tmp_path):
    root,cfg=fixture_dataset(tmp_path)
    audit=verify_portable_dataset(root,cfg)
    assert audit['frames']==3 and audit['boxes']==2
    assert audit['npz_sample_splits']==['test','train','val']
    assert audit['raw_source_access_required'] is False
    val=json.loads((root/'images/val/frame1.json').read_text())
    val['flags']['confirmed_negative']=False
    (root/'images/val/frame1.json').write_text(json.dumps(val))
    with pytest.raises(ValueError,match='flags'): verify_portable_dataset(root,cfg,hash_payloads=False)


def test_cross_split_group_is_rejected(tmp_path):
    root,cfg=fixture_dataset(tmp_path)
    rows=[json.loads(x) for x in (root/'frames.jsonl').read_text().splitlines()]
    rows[1]['group_id']=rows[0]['group_id']
    (root/'frames.jsonl').write_text(''.join(json.dumps(x)+'\n' for x in rows))
    with pytest.raises(ValueError,match='group'): verify_portable_dataset(root,cfg,hash_payloads=False)


def test_changed_checksum_and_duplicate_checksum_rows_are_rejected(tmp_path):
    root,cfg=fixture_dataset(tmp_path)
    assert verify_frozen_files(root)['state']=='passed'
    p=root/'checksums.sha256';text=p.read_text();p.write_text(text+text.splitlines()[0]+'\n')
    with pytest.raises(ValueError,match='table'): verify_frozen_files(root)
    p.write_text(text)
    (root/'labels/train/frame0.txt').write_text('')
    with pytest.raises(ValueError,match='changed frozen'): verify_frozen_files(root)


def test_relocated_unzip_hashes_and_preserves_human_changes(tmp_path):
    data,cfg=fixture_dataset(tmp_path)
    moved=tmp_path/'different-machine'/'handoff';moved.mkdir(parents=True)
    archive=moved/cfg['paths']['dataset_archive'];archive.parent.mkdir()
    with zipfile.ZipFile(archive,'w') as z:
        for p in data.rglob('*'):
            if p.is_file(): z.write(p,str(p.relative_to(data)))
    cfg['dataset']['archive_sha256']=digest(archive)
    (moved/'handoff_config.json').write_text(json.dumps(cfg))
    result=unpack_dataset(moved,minimum_free_gib=0)
    assert result['all_member_sha256']=='passed'
    assert verify_portable_dataset(moved/'dataset',cfg)['state']=='passed'
    (moved/'dataset/labels/train/frame0.txt').write_text('edited')
    with pytest.raises(ValueError,match='changed frozen'): unpack_dataset(moved,minimum_free_gib=0)


def test_archive_exact_coverage_and_crc(tmp_path):
    bundle=tmp_path/'bundle';bundle.mkdir();(bundle/'x.txt').write_text('ok')
    archive=tmp_path/'outer.zip'
    with zipfile.ZipFile(archive,'w') as z: z.write(bundle/'x.txt','bundle/x.txt')
    assert verify_outer_archive(archive,bundle)['crc']=='passed'
    with zipfile.ZipFile(archive,'a') as z: z.writestr('extra.txt','not allowed')
    with pytest.raises(ValueError,match='members'): verify_outer_archive(archive,bundle)


def test_dataset_archive_traversal_rejected_before_extraction(tmp_path):
    archive=tmp_path/'unsafe.zip'
    with zipfile.ZipFile(archive,'w') as z: z.writestr('../outside','bad')
    with zipfile.ZipFile(archive) as z:
        with pytest.raises(ValueError,match='Unsafe'): dataset_zip_entries(z)
