from dataclasses import asdict
import json
from pathlib import Path
import zipfile

import numpy as np
from PIL import Image
import pytest

from pallet.annotation import (Box, FrameAnnotation, Provenance, SceneAttributes,
                               annotation_digest)
from pallet.data.loader import policy_digest
from pallet.data.manifest import FrameRecord
from pallet.data.protocol import file_digest
from pallet.review_batches import display
import pallet.formal_dataset as formal
from pallet.human_labeling import _load_frame_index


def label(fid='a', split='test', *, negative=False):
    return FrameAnnotation(fid, f'g-{fid}', f'images/{split}/{fid}.png', 12, 8,
        status='negative' if negative else 'pos', boxes=[] if negative else [Box(1, 2, 5, 3)],
        scene=SceneAttributes(domain='d'), split=split, domain='d', source_anchor_hash=f'h-{fid}',
        provenance=[Provenance('human', actor='user_confirmed_archive_import')])


def receipt(f):
    return {'group_id': f.group_id, 'source_anchor_hash': f.source_anchor_hash, 'split': f.split,
            'width': f.width, 'height': f.height, 'annotation_digest': annotation_digest(f),
            'archive_sha256': 'archive-sha'}


def native_record(f):
    return FrameRecord(f.frame_id, f.group_id, 'v', {}, {'grid': [8, 12], 'domain_id': 'd'}, f.source_anchor_hash)


def returned_fixture(tmp_path, monkeypatch):
    source = tmp_path/'source'; source.mkdir()
    f = label(); r = native_record(f)
    row = {'frame_id': f.frame_id, 'group_id': f.group_id, 'split': f.split, 'domain': f.domain,
           'source_anchor_hash': f.source_anchor_hash, 'size': [12, 8], 'image': 'a.png', 'annotation': 'a.json'}
    doc = {'imageWidth': 12, 'imageHeight': 8, 'imagePath': 'a.png',
        'flags': {'unreviewed': True, 'ignore': True, 'proposal_only': True},
        'shapes': [{'label': 'pallet', 'shape_type': 'rectangle', 'points': [[1, 2], [6, 5]], 'flags': {'proposal_only': True}}],
        'pallet_prediction': {'frame_id': 'a', 'group_id': f.group_id, 'original_split': 'test',
            'domain': 'd', 'source_anchor_hash': f.source_anchor_hash}}
    Image.new('L', (12, 8), 64).save(source/'a.png')
    (source/'a.json').write_text(json.dumps(doc))
    monkeypatch.setattr(formal, '_load_frame_index', lambda _: ({}, {'a': row}, {'a.json': row}, 'manifest-sha', 'index-sha'))
    def run(docs, expected=None):
        archive = tmp_path/'return.zip'
        with zipfile.ZipFile(archive, 'w') as z:
            for name, value in docs:
                z.writestr(name, json.dumps(value))
        return formal.import_return(archive, source, {'a': r}, {f.group_id: 'test'}, set(),
                                    expected_ids={'a'} if expected is None else expected)
    return doc, run


def test_return_accepts_human_negative_despite_old_ignore_and_pending_flags(tmp_path, monkeypatch):
    doc, run = returned_fixture(tmp_path, monkeypatch)
    doc['shapes'] = []
    incoming, sources, differences, _ = run([('a.json', doc)])
    assert incoming['a'].status == 'negative' and incoming['a'].boxes == []
    assert len(incoming['a'].provenance) == 1 and incoming['a'].reviews == []
    assert sources['a']['annotation_digest'] == annotation_digest(incoming['a'])
    assert differences[0]['returned_flags']['unreviewed'] is True


@pytest.mark.parametrize('mutation', ['unknown', 'duplicate_frame', 'missing', 'group', 'split', 'rotation', 'bounds', 'nan'])
def test_return_rejects_missing_duplicate_and_changed_identity_or_boxes(tmp_path, monkeypatch, mutation):
    doc, run = returned_fixture(tmp_path, monkeypatch)
    docs = [('a.json', doc)]
    if mutation == 'unknown': docs = [('unknown.json', doc)]
    elif mutation == 'duplicate_frame': docs.append(('sub/a.json', doc))
    elif mutation == 'missing': docs = []
    elif mutation == 'group': doc['pallet_prediction']['group_id'] = 'other'
    elif mutation == 'split': doc['pallet_prediction']['original_split'] = 'train'
    elif mutation == 'rotation': doc['pallet_prediction']['pixel_rotation_degrees'] = 180
    elif mutation == 'bounds': doc['shapes'][0]['points'][1][0] = 13
    elif mutation == 'nan': doc['shapes'][0]['points'][1][0] = float('nan')
    with pytest.raises(ValueError): run(docs)


def test_formal_human_receipt_does_not_promote_machine_labels():
    f = label(); r = native_record(f); source = receipt(f)
    formal.validate_manual_frame(f, r, 'test', source)
    f.provenance.append(Provenance('detector'))
    with pytest.raises(ValueError, match='human corrections'): formal.validate_manual_frame(f, r, 'test', source)
    f.provenance.pop(); source['annotation_digest'] = 'other'
    with pytest.raises(ValueError, match='receipt mismatch'): formal.validate_manual_frame(f, r, 'test', source)


def test_portable_release_formats_masks_crc_and_modified_snapshot_protection(tmp_path, monkeypatch):
    output = tmp_path/'output'; bundle = output/'dataset'; source = tmp_path/'native'; protocol = tmp_path/'protocol'
    protocol.mkdir(); (protocol/'protocol.json').write_text('{}')
    labels = [label('a', 'train'), label('b', 'val', negative=True), label('c', 'test')]
    records = [native_record(f) for f in labels]; assignments = {f.group_id: f.split for f in labels}
    native, caches = {}, {}
    for f, record in zip(labels, records):
        data = np.zeros((8, 12, 4), np.float32); data[..., 1] = np.arange(96).reshape(8, 12); data[..., 3] = 1
        metadata = {'source_sha256': f.source_anchor_hash, 'policy_sha256': policy_digest(record.sensor_config)}
        sensor = tmp_path/f'{f.frame_id}.npz'; np.savez(sensor, frame=data, metadata=json.dumps(metadata))
        caches[f.frame_id] = {'path': str(sensor), 'sha256': file_digest(sensor), 'size': sensor.stat().st_size}
        (source/Path(f.image).parent).mkdir(parents=True, exist_ok=True)
        Image.fromarray(display(data[..., 1], data[..., 3], log=True)).save(source/f.image)
        depth = f'{f.frame_id}.depth.png'; Image.new('L', (12, 8)).save(source/depth)
        native[f.frame_id] = {'image': f.image, 'depth': depth, 'view_sha256': {'image': file_digest(source/f.image)}}
    formal.jsonl(bundle/'annotations/frames.jsonl', [asdict(f) for f in labels])
    formal.safe_json(bundle/'provenance/human_source_frames.json', {f.frame_id: receipt(f) for f in labels})
    for filename, value in [('orientation.json', []), ('reconstruction.json', []), ('cache.json', caches)]:
        (tmp_path/filename).write_text(json.dumps(value))
    config = {'output': str(output), 'protocol': str(protocol), 'cache_index': str(tmp_path/'cache.json'),
        'full_source_export': str(source), 'orientation_manifest': str(tmp_path/'orientation.json'),
        'reconstruction_manifest': str(tmp_path/'reconstruction.json')}
    imported = {'created_at': '2026-10-11T12:00:00+08:00', 'summary': {'frames': 3}, 'input_binding': {}}
    monkeypatch.setattr(formal, 'prepare', lambda _: (config, imported))
    monkeypatch.setattr(formal, 'load_protocol', lambda _: (records, assignments, {'split_seed': 42, 'files': {}}))
    monkeypatch.setattr(formal, '_load_frame_index', lambda _: ({}, native, {}, '', ''))
    delivery = formal.build('fixture')
    from ultralytics.data.utils import check_det_dataset
    parsed = check_det_dataset(str((bundle/'dataset.yaml').resolve()), autodownload=False)
    assert Path(parsed['train']) == (bundle/'images/train').resolve()
    assert Path(parsed['val']) == (bundle/'images/val').resolve()
    assert Path(parsed['test']) == (bundle/'images/test').resolve()
    assert delivery['dataset_audit']['frames'] == 3 and delivery['dataset_audit']['boxes'] == 2
    assert delivery['archive_audit']['crc'] == 'passed'
    assert (bundle/'labels/val/b.txt').read_text() == ''
    val = formal.read(bundle/'annotations/val.coco.json')
    assert len(val['images']) == 1 and val['annotations'] == []
    assert formal.read(bundle/'images/test/c.json')['flags']['human_reviewed'] is True
    assert delivery['final_test_scored'] is False
    assert formal.build('fixture')['archive_audit']['sha256'] == delivery['archive_audit']['sha256']
    edited = bundle/'images/test/c.json'; edited.write_text('{}')
    with pytest.raises(ValueError, match='Payload modified'): formal.build('fixture')
    assert edited.read_text() == '{}'


def test_release_zip_rejects_changed_payload_and_duplicate_members(tmp_path):
    bundle = tmp_path/'bundle'; bundle.mkdir(); (bundle/'a.txt').write_text('accepted')
    archive = tmp_path/'package.zip'
    with zipfile.ZipFile(archive, 'w') as z: z.writestr('a.txt', 'different')
    with pytest.raises(ValueError, match='payload mismatch'): formal.verify_archive(archive, bundle)
    with zipfile.ZipFile(archive, 'w') as z:
        z.writestr('a.txt', 'accepted'); z.writestr('sub/a.txt', 'extra')
    with pytest.raises(ValueError, match='membership'): formal.verify_archive(archive, bundle)


def test_formal_manifest_read_adapter_keeps_frozen_bytes_and_reference_scope(tmp_path):
    row = {'frame_id': 'f', 'image': 'images/test/f.png', 'annotation': 'images/test/f.json',
           'size': [12, 8], 'human_reviewed': True, 'split': 'test'}
    formal.jsonl(tmp_path/'frames.jsonl', [row])
    manifest = {'kind': 'formal_user_reviewed_pallet_dataset', 'state': 'frozen', 'summary': {'frames': 1},
        'files': {'frames.jsonl': file_digest(tmp_path/'frames.jsonl'), 'protocol/protocol.json': 'protocol-sha'},
        'independent_blind_reference': False, 'historical_assistant_final_test_eligible': False}
    formal.safe_json(tmp_path/'manifest.json', manifest)
    before = file_digest(tmp_path/'manifest.json')
    parsed, index, _, _, _ = _load_frame_index(tmp_path)
    assert index == {'f': row}
    assert parsed['binding']['protocol_sha256'] == 'protocol-sha'
    assert parsed['independent_blind_reference'] is False
    assert file_digest(tmp_path/'manifest.json') == before
    with (tmp_path/'frames.jsonl').open('a') as stream: stream.write('\n')
    with pytest.raises(ValueError, match='index hash'): _load_frame_index(tmp_path)
