from dataclasses import asdict
import json
import zipfile

import pytest
from PIL import Image

from pallet.annotation import AnnotationSet, Box, FrameAnnotation, Provenance, annotation_digest, save_jsonl
from pallet.data.manifest import FrameRecord
from pallet.data.protocol import file_digest
from pallet.experiments.dataset import PalletDataset
from pallet.data.loader import NormalizationStats
import pallet.human_labeling as human


def _frame(frame_id='f', split='train', *, boxes=None, status='pos'):
    return FrameAnnotation(frame_id, f'g-{frame_id}', 'native.pcd', 32, 24,
        status=status, boxes=list(boxes if boxes is not None else [Box(2, 3, 20, 15)]),
        split=split, source_anchor_hash=f'source-{frame_id}',
        provenance=[Provenance('human', actor='user_confirmed_archive_import')])


def test_anylabeling_rectangles_normalize_corner_order_and_check_bounds():
    result = human._decode_rectangles({'shapes': [
        {'label': 'pallet', 'shape_type': 'rectangle', 'points': [[22, 18], [2, 3]]},
        {'label': 'pallet', 'shape_type': 'rectangle', 'points': [[2, 3], [22, 3], [22, 18], [2, 18]]}]}, 32, 24)
    assert result == [Box(2, 3, 20, 15), Box(2, 3, 20, 15)]
    assert human._decode_rectangles({'shapes': []}, 32, 24) == []
    with pytest.raises(ValueError, match='outside image'):
        human._decode_rectangles({'shapes': [{'label': 'pallet', 'shape_type': 'rectangle',
            'points': [[20, 4], [33, 17]]}]}, 32, 24)


@pytest.mark.parametrize('shape', [
    {'label': 'forklift', 'shape_type': 'rectangle', 'points': [[1, 1], [10, 10]]},
    {'label': 'pallet', 'shape_type': 'polygon', 'points': [[1, 1], [10, 10]]},
    {'label': 'pallet', 'shape_type': 'rectangle', 'points': [[float('nan'), 1], [10, 10]]},
])
def test_import_rejects_wrong_class_shape_and_nonfinite_points(shape):
    with pytest.raises(ValueError):
        human._decode_rectangles({'shapes': [shape]}, 32, 24)


def test_human_training_manifest_accepts_only_bound_train_snapshot(tmp_path, monkeypatch):
    protocol = tmp_path/'protocol'
    protocol.mkdir()
    (protocol/'protocol.json').write_text('{}')
    records = [FrameRecord('f', 'g-f', 'all', {}, {'grid': [24, 32]}, 'source-f')]
    monkeypatch.setattr(human, 'load_protocol', lambda path: (records, {'g-f': 'train'}, {}))
    labels_path = tmp_path/'annotations.jsonl'
    labels = AnnotationSet([_frame()])
    save_jsonl(labels, labels_path)
    entry = {'group_id': 'g-f', 'source_anchor_hash': 'source-f', 'split': 'train',
        'status': 'pos', 'annotation_digest': annotation_digest(labels.frames['f'])}
    manifest = {'kind': 'explicit_user_confirmed_manual_training_labels',
        'protocol_sha256': file_digest(protocol/'protocol.json'),
        'annotation_snapshot_sha256': file_digest(labels_path),
        'training_frame_ids': ['f'], 'training_frames': 1, 'source_frames': {'f': entry}}
    manifest_path = tmp_path/'human_training_manifest.json'
    manifest_path.write_text(json.dumps(manifest))
    assert human.validate_human_training_manifest(manifest_path, manifest, labels, protocol) == frozenset({'f'})
    entry['source_anchor_hash'] = 'other'
    with pytest.raises(ValueError, match='identity/label mismatch'):
        human.validate_human_training_manifest(manifest_path, manifest, labels, protocol)


def test_human_training_manifest_cannot_attest_val_labels(tmp_path, monkeypatch):
    protocol = tmp_path/'protocol'
    protocol.mkdir()
    (protocol/'protocol.json').write_text('{}')
    frame = _frame(split='val')
    records = [FrameRecord('f', 'g-f', 'all', {}, {'grid': [24, 32]}, 'source-f')]
    monkeypatch.setattr(human, 'load_protocol', lambda path: (records, {'g-f': 'val'}, {}))
    labels_path = tmp_path/'annotations.jsonl'
    labels = AnnotationSet([frame])
    save_jsonl(labels, labels_path)
    manifest = {'kind': 'explicit_user_confirmed_manual_training_labels',
        'protocol_sha256': file_digest(protocol/'protocol.json'),
        'annotation_snapshot_sha256': file_digest(labels_path),
        'training_frame_ids': ['f'], 'training_frames': 1,
        'source_frames': {'f': {'group_id': 'g-f', 'source_anchor_hash': 'source-f',
            'split': 'train', 'status': 'pos', 'annotation_digest': annotation_digest(frame)}}}
    with pytest.raises(ValueError, match='identity/label mismatch'):
        human.validate_human_training_manifest(tmp_path/'human.json', manifest, labels, protocol)


def test_dataset_uses_explicit_manual_train_eligibility_but_not_for_val():
    labels = AnnotationSet([_frame(), _frame('v', split='val')])
    train = FrameRecord('f', 'g-f', 'all', {}, {'grid': [24, 32]}, 'source-f')
    val = FrameRecord('v', 'g-v', 'all', {}, {'grid': [24, 32]}, 'source-v')
    dataset = PalletDataset([train, val], labels, NormalizationStats(1, 1),
                            require_review=True, manual_training_ids={'f'})
    assert [frame.frame_id for _, frame in dataset.rows] == ['f']
    with pytest.raises(ValueError, match='non-train frame'):
        PalletDataset([val], labels, NormalizationStats(1, 1),
                      require_review=True, manual_training_ids={'v'})


def test_manual_validation_dataset_is_allowed_only_in_validation_role():
    labels = AnnotationSet([_frame('tr'), _frame('va')])
    train = FrameRecord('tr', 'g-tr', 'all', {}, {'grid': [24, 32]}, 'source-tr')
    val = FrameRecord('va', 'g-va', 'all', {}, {'grid': [24, 32]}, 'source-va')
    training = PalletDataset([train], labels, NormalizationStats(1, 1),
        require_review=True, manual_training_ids={'tr'}, manual_validation_ids={'va'},
        manual_role='train')
    validation = PalletDataset([val], labels, NormalizationStats(1, 1),
        require_review=True, manual_training_ids={'tr'}, manual_validation_ids={'va'},
        manual_role='val')
    assert [frame.frame_id for _, frame in training.rows] == ['tr']
    assert [frame.frame_id for _, frame in validation.rows] == ['va']
    with pytest.raises(ValueError, match='wrong role'):
        PalletDataset([val], labels, NormalizationStats(1, 1),
            require_review=True, manual_validation_ids={'va'}, manual_role='train')


def test_group_holdout_is_deterministic_domain_stratified_and_keeps_negatives_in_train():
    frames = []
    records = {}
    for domain, prefix in [('d1', 'a'), ('d2', 'b')]:
        for group_index in range(4):
            group_id = f'{prefix}-g{group_index}'
            status = 'negative' if prefix == 'b' and group_index == 0 else 'pos'
            for frame_index in range(2):
                frame_id = f'{group_id}-f{frame_index}'
                frame = _frame(frame_id, status=status,
                    boxes=[] if status == 'negative' else [Box(2, 3, 20, 15)])
                frame.group_id = group_id
                frame.domain = domain
                frame.provenance = [Provenance('human', actor='user_confirmed_archive_import')]
                frames.append(frame)
                records[frame_id] = FrameRecord(frame_id, group_id, domain, {},
                    {'grid': [24, 32], 'domain_id': domain}, frame.source_anchor_hash)
    first = human._choose_group_holdout(frames, records, seed=42, fraction=.25)
    second = human._choose_group_holdout(frames, records, seed=42, fraction=.25)
    assert first == second
    val_ids, val_groups, by_domain = first
    val_frames = [frame for frame in frames if frame.frame_id in val_ids]
    assert len(val_ids) == 4
    assert set(by_domain) == {'d1', 'd2'}
    assert not any(frame.status == 'negative' for frame in val_frames)
    assert len(val_groups) == 2


def test_group_holdout_places_negatives_in_both_splits_when_groups_allow():
    frames, records = [], {}
    for i, status in enumerate(['negative', 'negative', 'pos', 'pos']):
        group_id = f'g{i}'
        frame = _frame(f'f{i}', status=status,
                       boxes=[] if status == 'negative' else [Box(2, 3, 20, 15)])
        frame.group_id = group_id
        frame.domain = 'camera-a'
        frame.source_anchor_hash = f'source-{i}'
        frames.append(frame)
        records[frame.frame_id] = FrameRecord(frame.frame_id, group_id, 'camera-a', {},
            {'grid': [24, 32], 'domain_id': 'camera-a'}, frame.source_anchor_hash)
    val_ids, val_groups, counts = human._choose_group_holdout(
        frames, records, seed=42, fraction=.5)
    val = [frame for frame in frames if frame.frame_id in val_ids]
    train = [frame for frame in frames if frame.frame_id not in val_ids]
    assert len(val) == 2
    assert sum(frame.status == 'negative' for frame in val) == 1
    assert sum(frame.status == 'negative' for frame in train) == 1
    assert {frame.group_id for frame in val}.isdisjoint(
        {frame.group_id for frame in train})
    assert counts['camera-a']['validation_negatives'] == 1


def test_import_manual_accepts_multiple_export_schemas_and_empty_negatives(tmp_path, monkeypatch):
    protocol = tmp_path/'protocol'
    protocol.mkdir()
    (protocol/'protocol.json').write_text('{}')
    frame_ids = ['raw-frame', 'proposal-frame']
    records = [FrameRecord(fid, f'group-{fid}', 'all', {}, {'grid': [24, 32]}, f'source-{fid}')
               for fid in frame_ids]
    assignments = {record.group_id: 'train' for record in records}
    monkeypatch.setattr(human, 'load_protocol', lambda _: (records, assignments, {}))
    archives = []
    for fid, schema, reviewed_shapes in [
            ('raw-frame', 'pallet_export', [{'label': 'pallet', 'shape_type': 'rectangle',
                'points': [[2, 3], [22, 18]]}]),
            ('proposal-frame', 'pallet_prediction', [])]:
        source = tmp_path/f'source-{fid}'
        annotation = source/'images/train'/f'{fid}.json'
        annotation.parent.mkdir(parents=True)
        metadata = {'frame_id': fid, 'source_anchor_hash': f'source-{fid}',
                    'group_id': f'group-{fid}'}
        baseline = {'imageWidth': 32, 'imageHeight': 24, 'imagePath': f'{fid}.png',
            'shapes': reviewed_shapes, schema: metadata}
        annotation.write_text(json.dumps(baseline))
        image_path = source/'images/train'/f'{fid}.png'
        Image.new('L', (32, 24)).save(image_path)
        row = {'frame_id': fid, 'group_id': f'group-{fid}', 'split': 'train',
            'domain': 'camera-a', 'source_anchor_hash': f'source-{fid}',
            'annotation': f'images/train/{fid}.json', 'image': f'images/train/{fid}.png',
            'depth': f'auxiliary/train/{fid}.depth.png',
            'validity': f'auxiliary/train/{fid}.validity.png'}
        if schema == 'pallet_export':
            row.update({'size': [32, 24], 'source_status': 'proposal',
                        'baseline_json_sha256': file_digest(annotation)})
        else:
            row.update({'baseline_annotation_sha256': file_digest(annotation),
                        'view_sha256': {'image': file_digest(image_path)}})
        (source/'frames.jsonl').write_text(json.dumps(row)+'\n')
        from pallet.data.protocol import write_json
        manifest_name = 'manifest.json' if schema == 'pallet_export' else 'package_manifest.json'
        manifest = {'kind': 'source-fixture', 'frames': 1,
            'frames_index_sha256': file_digest(source/'frames.jsonl')}
        manifest['binding' if schema == 'pallet_export' else 'prediction_binding'] = {
            'protocol_sha256': file_digest(protocol/'protocol.json')}
        write_json(source/manifest_name, manifest)
        archive = tmp_path/f'{fid}.zip'
        correction = {'imageWidth': 32, 'imageHeight': 24, 'imagePath': f'{fid}.png',
                      'shapes': reviewed_shapes, schema: metadata}
        with zipfile.ZipFile(archive, 'w') as bundle:
            bundle.writestr(f'{fid}.json', json.dumps(correction))
        archives.append({'archive': str(archive), 'source_export': str(source),
                         'expected_frames': 1})
    config_path = tmp_path/'corrections.json'
    config = {'kind': 'user_confirmed_manual_correction_archives_v1',
        'all_frames_reviewed_by_user': True,
        'unchanged_proposals_accepted_after_manual_review': True,
        'empty_frames_confirmed_negative_by_user': True,
        'sources': archives,
        'expected_total': {'frames': 2, 'positive_frames': 1,
                           'negative_frames': 1, 'boxes': 1, 'groups': 2}}
    config_path.write_text(json.dumps(config))
    result = human.import_manual_correction_archives(corrections_config=config_path,
        protocol=protocol, output=tmp_path/'manual-output')
    snapshot = human.load_jsonl(tmp_path/'manual-output/inputs/manual_annotations.jsonl')
    assert result['frame_count'] == 2
    assert snapshot.frames['raw-frame'].status == 'pos'
    assert snapshot.frames['proposal-frame'].status == 'negative'
    assert snapshot.frames['raw-frame'].boxes == [Box(2, 3, 20, 15)]
    assert snapshot.frames['proposal-frame'].provenance[0].actor == 'user_confirmed_archive_import'


def test_candidate_package_audit_checks_flags_bounds_pairs_and_crc(tmp_path, monkeypatch):
    import hashlib
    from PIL import Image
    from pallet.data.protocol import write_json

    protocol = tmp_path/'protocol'
    protocol.mkdir()
    record = FrameRecord('remaining', 'group-remaining', 'camera-a', {},
        {'grid': [24, 32], 'domain_id': 'camera-a'}, 'source-remaining')
    rows = [
        {'frame_id': 'reviewed', 'group_id': 'group-reviewed', 'split': 'train',
         'domain': 'camera-a', 'source_anchor_hash': 'source-reviewed',
         'annotation': 'images/train/reviewed.json', 'image': 'images/train/reviewed.png',
         'depth': 'auxiliary/train/reviewed.depth.png', 'validity': 'auxiliary/train/reviewed.validity.png',
         'size': [32, 24]},
        {'frame_id': 'remaining', 'group_id': 'group-remaining', 'split': 'train',
         'domain': 'camera-a', 'source_anchor_hash': 'source-remaining',
         'annotation': 'images/train/remaining.json', 'image': 'images/train/remaining.png',
         'depth': 'auxiliary/train/remaining.depth.png', 'validity': 'auxiliary/train/remaining.validity.png',
         'size': [32, 24]}]
    source = tmp_path/'source'
    source.mkdir()
    (source/'frames.jsonl').write_text(''.join(json.dumps(row)+'\n' for row in rows))
    write_json(source/'manifest.json', {'frames': 2,
        'frames_index_sha256': file_digest(source/'frames.jsonl'),
        'binding': {'protocol_sha256': 'protocol'}})
    monkeypatch.setattr(human, 'load_protocol', lambda _: ([record],
        {'group-reviewed': 'train', 'group-remaining': 'train'}, {}))
    monkeypatch.setattr(human, '_validated_manual_partition', lambda *_: (
        AnnotationSet(), {'reviewed'}, set(), {}, {'source_archive_sha256': 'archive-set'}))
    output = tmp_path/'assistant'
    (output/'inputs').mkdir(parents=True)
    (output/'inputs/annotations.jsonl').write_text('')
    root = output/'anylabeling'
    image_path = root/'images/train/remaining.png'
    depth_path = root/'auxiliary/train/remaining.depth.png'
    validity_path = root/'auxiliary/train/remaining.validity.png'
    image_path.parent.mkdir(parents=True)
    depth_path.parent.mkdir(parents=True)
    Image.new('L', (32, 24), 128).save(image_path)
    Image.new('I;16', (32, 24), 1000).save(depth_path)
    Image.new('L', (32, 24), 255).save(validity_path)
    document = {'imageWidth': 32, 'imageHeight': 24, 'imagePath': image_path.name,
        'flags': {'human_reviewed': False, 'unreviewed': True, 'proposal_only': True,
            'confirmed_negative': False, 'ignore': False, 'machine_resolved': False},
        'shapes': [{'label': 'pallet', 'shape_type': 'rectangle',
            'points': [[2, 3], [22, 18]]}],
        'pallet_prediction': {'frame_id': 'remaining', 'group_id': 'group-remaining',
            'source_anchor_hash': 'source-remaining', 'status': 'proposal',
            'confidence': [0.8], 'prediction_only': True}}
    label_path = root/'images/train/remaining.json'
    label_path.write_text(json.dumps(document))
    package_row = {'frame_id': 'remaining', 'group_id': 'group-remaining', 'split': 'train',
        'domain': 'camera-a', 'status': 'proposal', 'proposal_count': 1, 'confidences': [0.8],
        'image': 'images/train/remaining.png', 'annotation': 'images/train/remaining.json',
        'depth': 'auxiliary/train/remaining.depth.png',
        'validity': 'auxiliary/train/remaining.validity.png',
        'source_anchor_hash': 'source-remaining', 'independent_reference': False,
        'baseline_annotation_sha256': file_digest(label_path),
        'view_sha256': {'image': file_digest(image_path), 'depth': file_digest(depth_path),
                        'validity': file_digest(validity_path)}}
    frame_list = root/'frames.jsonl'
    frame_list.write_text(json.dumps(package_row)+'\n')
    write_json(root/'package_manifest.json', {'frames': 1,
        'split_counts': {'train': 1}, 'status_counts': {
            'train': {'proposal': 1}, 'val': {}, 'test': {}},
        'frames_index_sha256': file_digest(frame_list),
        'validation_test_outputs_are_independent_references': False})
    archive = output/'human_correction_remaining_1_v1.zip'
    with zipfile.ZipFile(archive, 'w') as bundle:
        for i in range(9):
            bundle.writestr(f'member-{i}', b'candidate')
    audit = human.audit_candidate_package(output=output, source_export=source,
        protocol=protocol, archive=archive)
    assert audit['state'] == 'passed'
    assert audit['frames'] == 1
    assert audit['manual_overlap'] == 0
    assert audit['zip_members'] == 9
    assert audit['zip_crc'] == 'passed'


def test_manual_validation_manifest_binds_only_heldout_user_labels(tmp_path, monkeypatch):
    protocol = tmp_path/'protocol'
    protocol.mkdir()
    (protocol/'protocol.json').write_text('{}')
    train = _frame('train')
    val = _frame('val')
    val.group_id = 'g-val'
    records = [
        FrameRecord('train', 'g-train', 'd', {}, {'grid': [24, 32]}, 'source-train'),
        FrameRecord('val', 'g-val', 'd', {}, {'grid': [24, 32]}, 'source-val')]
    train.source_anchor_hash = 'source-train'
    val.source_anchor_hash = 'source-val'
    monkeypatch.setattr(human, 'load_protocol', lambda path: (
        records, {'g-train': 'train', 'g-val': 'train'}, {}))
    labels_path = tmp_path/'annotations.jsonl'
    labels = AnnotationSet([train, val])
    save_jsonl(labels, labels_path)
    entry = {'group_id': val.group_id, 'source_anchor_hash': val.source_anchor_hash,
        'split': 'train', 'status': 'pos', 'annotation_digest': annotation_digest(val)}
    manifest = {'kind': 'explicit_user_confirmed_manual_validation_holdout',
        'protocol_sha256': file_digest(protocol/'protocol.json'),
        'annotation_snapshot_sha256': file_digest(labels_path), 'source_archive_sha256': 'archive',
        'validation_frame_ids': ['val'], 'validation_frames': 1, 'source_frames': {'val': entry}}
    manifest_path = tmp_path/'human_validation_manifest.json'
    manifest_path.write_text(json.dumps(manifest))
    assert human.validate_human_validation_manifest(manifest_path, manifest, labels,
        protocol, {'train'}) == frozenset({'val'})
    with pytest.raises(ValueError, match='overlaps training'):
        human.validate_human_validation_manifest(manifest_path, manifest, labels,
            protocol, {'train', 'val'})


def test_runtime_estimate_uses_first_ten_recorded_epochs():
    rows = [{'epoch_seconds': seconds} for seconds in range(10, 20)]
    result = human._epoch_timing_estimate(rows, 300)
    assert result['measured_epochs'] == 10
    assert result['median_seconds_per_epoch'] == 14.5
    assert result['estimated_remaining_seconds'] == 290*14.5
