import copy
import json
from pathlib import Path
import shutil
import zipfile

import numpy as np
from PIL import Image
import pytest

import pallet.cluster_annotation_seed as module
from pallet.cluster_annotation_seed import ignore_regions, main, native_box, select_targets
from pallet.fork_face_anylabeling import digest, encoded, rasterize


def row(cluster, frame, *, split='train', group=None, domain='d', box=0):
    return {'target_id': f'{frame}::box:{box:04d}', 'target_key': f'{frame}_p{box}',
            'appearance_cluster': cluster, 'frame_id': frame, 'split': split,
            'group_id': group or frame, 'domain': domain, 'box_index': box,
            'bbox': {'x': 2.25, 'y': 3.5, 'width': 11.1, 'height': 8.2},
            'review_reasons': ['candidate_not_confirmed']}


def test_matching_lookahead_reserves_scarce_frames_and_reproduces():
    rows = [row(0, 'shared'), row(0, 'a'), row(0, 'b'), row(1, 'shared', box=1), row(1, 'c')]
    features = np.array([[0], [-2], [2], [0], [3]])
    first, coverage, quotas = select_targets(rows, features, 2, 42)
    second, _, _ = select_targets(rows, features, 2, 42)
    assert first == second
    assert {r['frame_id'] for r in first if r['appearance_cluster'] == 0} == {'a', 'b'}
    assert len({r['frame_id'] for r in first}) == 4
    assert quotas == {'A00/train': 2, 'A01/train': 2}
    assert all(c['selected_targets'] == 2 for c in coverage)


def test_group_then_domain_balance_before_farthest_point():
    rows = [row(0, 'a', group='same'), row(0, 'b', group='same'),
            row(0, 'c', group='other'), row(0, 'd', group='third')]
    features = np.array([[0], [100], [1], [-1]])
    chosen, _, _ = select_targets(rows, features, 3, 42)
    assert len({r['group_id'] for r in chosen}) == 3
    assert chosen[0]['selection_distance_kind'] == 'cluster_center'
    assert all(r['selection_distance_kind'] == 'nearest_selected' for r in chosen[1:])


def test_train_then_val_then_test_priority_and_noise_exclusion():
    rows = [row(0, 'train'), row(0, 'val', split='val'), row(0, 'test', split='test'),
            row(1, 'test2', split='test'), row(1, 'test3', split='test'), row(-1, 'noise')]
    chosen, _, quotas = select_targets(rows, np.arange(6)[:, None], 2)
    assert [r['split'] for r in chosen] == ['train', 'val', 'test', 'test']
    assert 'noise' not in {r['frame_id'] for r in chosen}
    assert quotas == {'A00/train': 1, 'A00/val': 1, 'A01/test': 2}


def test_shortage_and_cross_cluster_frame_collision_are_explicit():
    with pytest.raises(ValueError, match='insufficient distinct'):
        select_targets([row(0, 'x'), row(0, 'x', box=1)], np.ones((2, 1)), 2)
    with pytest.raises(ValueError, match='globally unique'):
        select_targets([row(0, 'x'), row(1, 'x', box=1)], np.ones((2, 1)), 1)
    with pytest.raises(ValueError, match='do not align'):
        select_targets([row(0, 'x')], np.array([[np.nan]]), 1)


def test_float_boxes_round_outward_and_ignore_overlap_stays_ignored():
    bbox = {'x': -.2, 'y': 2.4, 'width': 7.6, 'height': 5.2}
    assert native_box(bbox, (14, 20)) == [0, 2, 8, 8]
    selected = {'target_id': 'chosen', 'bbox': bbox}
    other = {'target_id': 'other', 'bbox': {'x': 5.2, 'y': 3.1, 'width': 10.4, 'height': 5.1}}
    mask, ignored = ignore_regions([selected, other], 'chosen', (14, 20))
    assert mask[3:9, 5:16].all() and mask.sum() == 6*11
    assert ignored[0]['bbox'] == other['bbox']
    assert mask[4, 6]  # selected-positive overlap must not clear the ignore region


@pytest.fixture
def seed_input(tmp_path):
    clustering, source = tmp_path/'clustering', tmp_path/'segmentation'
    clustering.mkdir(); source.mkdir()
    inputs = [('a', 0, 'train'), ('b', 0, 'train'), ('c', 1, 'test'),
              ('d', 1, 'test'), ('e', 2, 'train'), ('f', 2, 'val')]
    rows, frames = [], []
    for name, cluster, split in inputs:
        parent = row(cluster, name, split=split)
        rows.append(parent)
        image_path = f'faces/images/{split}/{name}.png'
        path = source/image_path; path.parent.mkdir(parents=True, exist_ok=True)
        Image.fromarray(np.arange(280, dtype=np.uint16).reshape(14, 20).astype(np.uint8)).save(path)
        parents = [{'target_id': parent['target_id'], 'bbox': parent['bbox'], 'pallet_group_id': 1,
                    'status': 'candidate_needs_review', 'review_reasons': ['forkability_not_confirmed']}]
        if name == 'a':
            other = row(-1, name, split=split, box=1)
            other['bbox'] = {'x': 10.6, 'y': 2.4, 'width': 6.2, 'height': 8.1}
            rows.append(other)
            parents.append({'target_id': other['target_id'], 'bbox': other['bbox'], 'pallet_group_id': 2,
                            'status': 'candidate_needs_review', 'review_reasons': []})
        groups, shapes = [], []
        face_count = 0 if name == 'e' else (2 if name == 'a' else 1)
        for i in range(face_count):
            points = [[3+i*4, 4], [12+i, 4], [12+i, 10], [3+i*4, 10]]
            mask = rasterize(points, (14, 20))
            relative = f'reference_masks/faces/{split}/{name}_f{i+1}.png'
            path = source/relative; path.parent.mkdir(parents=True, exist_ok=True)
            Image.fromarray(mask.astype(np.uint8)*255).save(path)
            groups.append({'group_id': i+1, 'face_id': i+1, 'pallet_group_id': 1,
                           'target_id': parent['target_id'], 'mask': relative, 'mask_sha256': digest(path),
                           'proposal_only': True, 'mask_human_reviewed': False})
            shapes.append({'label': module.LABEL, 'points': points, 'group_id': i+1, 'shape_type': 'polygon',
                           'text': '', 'description': '', 'flags': {'proposal_only': True, 'mask_human_reviewed': False},
                           'pallet_mask_part': {'face_id': i+1, 'pallet_group_id': 1, 'part_index': 0}})
        if name == 'a':
            points = [[11, 3], [16, 3], [16, 9], [11, 9]]
            groups.append({'group_id': 9, 'face_id': 9, 'pallet_group_id': 2, 'target_id': rows[-1]['target_id'],
                           'mask': 'unused_reference.png'})
            shapes.append({'label': module.LABEL, 'points': points, 'group_id': 9, 'shape_type': 'polygon',
                           'text': '', 'flags': {'proposal_only': True, 'mask_human_reviewed': False},
                           'pallet_mask_part': {'face_id': 9, 'pallet_group_id': 2, 'part_index': 0}})
        doc = {'version': '0.4.43', 'imagePath': name+'.png', 'imageData': None, 'imageWidth': 20,
               'imageHeight': 14, 'text': '', 'checked': False, 'flags': {}, 'shapes': shapes,
               'pallet_segmentation': {'view': 'faces', 'parents': parents, 'groups': groups,
                    'pixel_rotation_degrees': 180 if name == 'a' else 0,
                    'reconstructed_intensity': name == 'a', 'original_split': split,
                    'frame_id': name, 'frame_key': name}}
        annotation = f'faces/images/{split}/{name}.json'
        (source/annotation).write_bytes(encoded(doc))
        frames.append({'frame_id': name, 'frame_key': name, 'width': 20, 'height': 14,
                       'views': {'faces': {'image': image_path, 'annotation': annotation}}})
    (clustering/'targets.jsonl').write_bytes(b''.join(encoded(r) for r in rows))
    np.savez_compressed(clustering/'features.npz', appearance_indices=np.arange(len(rows)),
                        appearance_features=np.arange(len(rows), dtype=float)[:, None])
    (clustering/'clusters.json').write_bytes(encoded({'appearance': [{'cluster_id': i} for i in range(3)]}))
    (clustering/'checksums.sha256').write_text(''.join(f'{digest(p)}  {p.name}\n' for p in sorted(clustering.iterdir())))
    (source/'frames.jsonl').write_bytes(b''.join(encoded(r) for r in frames))
    (source/'targets.jsonl').write_bytes(b''.join(encoded(r) for r in rows))
    real = module.DEFAULT_SOURCE/'source'
    config = json.loads((real/'config.json').read_text())
    (source/'source').mkdir()
    for filename in ['config.json', 'anylabeling_shape.py', 'anylabeling_label_file.py']:
        shutil.copyfile(real/filename, source/'source'/filename)
    (source/'manifest.json').write_bytes(encoded({'binding': {'config': config}}))
    hashes = {str(p.relative_to(source)): digest(p) for p in source.rglob('*') if p.is_file()}
    (source/'verified_assets.json').write_bytes(encoded({'task_hash': 'source', 'files': hashes}))
    argv = ['--clustering', str(clustering), '--source', str(source), '--output', str(tmp_path/'output'),
            '--per-cluster', '2']
    return clustering, source, tmp_path/'output', argv


def test_complete_export_official_format_native_grid_and_portable_zip(seed_input):
    clustering, source, output, argv = seed_input
    main(argv)
    receipt = json.loads((output/'delivery_manifest.json').read_text())
    summary = receipt['summary']
    assert summary['targets'] == summary['frames'] == 6
    assert summary['appearance_classes'] == 3 and summary['face_instances'] == 6
    assert summary['source_splits'] == {'train': 3, 'val': 1, 'test': 2}
    assert summary['redraw_targets'] == 1
    assert summary['frames_with_face_overlap'] == summary['frames_with_ignore_overlap'] == 1
    assert receipt['crc_passed'] and receipt['all_member_sha256_passed']
    assert receipt['portable_load_save_documents'] == 6
    assert receipt['validation']['official_anylabeling_load_save_documents'] == 6
    doc = json.loads((output/'delivery/A00/images/a.json').read_text())
    assert {s['group_id'] for s in doc['shapes']} == {1, 2}
    meta = doc['pallet_segmentation']
    assert len(meta['parents']) == len(meta['ignored_parents']) == 1
    assert meta['pixel_rotation_degrees'] == 180
    assert meta['parents'][0]['bbox']['x'] == 2.25
    assert doc['flags']['partial_annotation'] and doc['flags']['ignore_overlap_needs_review']
    assert not doc['flags']['segmentation_training_eligible']
    original = json.loads((source/'faces/images/train/a.json').read_text())
    assert [s['points'] for s in doc['shapes']] == [s['points'] for s in original['shapes'] if s['group_id'] in (1, 2)]
    public = [json.loads(line) for line in (output/'delivery/targets.jsonl').open()]
    assert sum(len(r['face_previews']) for r in public) == summary['face_instances']
    assert (output/'delivery/A00/images/a.png').read_bytes() == (source/'faces/images/train/a.png').read_bytes()
    unresolved = json.loads((output/'delivery/A02/images/e.json').read_text())
    assert not unresolved['shapes'] and unresolved['flags']['unresolved_positive']
    assert unresolved['flags']['redraw_required'] and not unresolved['flags']['confirmed_negative']
    with zipfile.ZipFile(output/receipt['archive_name']) as archive:
        assert 'gallery.html' in archive.namelist() and 'ignore_masks/A00/a.png' in archive.namelist()
    sha = receipt['archive_sha256']; main(argv)
    assert json.loads((output/'delivery_manifest.json').read_text())['archive_sha256'] == sha


@pytest.mark.parametrize('stage', ['export', 'verify', 'package', 'all'])
def test_human_edits_are_never_overwritten(seed_input, stage):
    _, _, output, argv = seed_input
    main(argv)
    path = output/'delivery/A00/images/a.json'
    doc = json.loads(path.read_text()); doc['flags']['human_reviewed'] = True
    path.write_bytes(encoded(doc)); human = path.read_bytes()
    with pytest.raises(ValueError, match='human edits'):
        main(argv+['--stage', stage])
    assert path.read_bytes() == human


def test_changed_frozen_input_is_rejected(seed_input):
    _, source, _, argv = seed_input
    main(argv+['--stage', 'select'])
    (source/'frames.jsonl').write_text('{}\n')
    with pytest.raises(ValueError, match='Frozen source changed'):
        main(argv+['--stage', 'select'])


def test_partial_export_protects_preexisting_human_json(seed_input):
    _, _, output, argv = seed_input
    main(argv+['--stage', 'select'])
    path = output/'delivery/A00/images/a.json'; path.parent.mkdir(parents=True)
    path.write_bytes(b'{"human_reviewed":true}')
    with pytest.raises(ValueError, match='human edits'):
        main(argv+['--stage', 'export'])
    assert path.read_bytes() == b'{"human_reviewed":true}'


def test_input_directory_cannot_be_used_as_output(seed_input):
    _, source, _, argv = seed_input
    with pytest.raises(ValueError, match='separate'):
        main(argv+['--output', str(source/'accidental')])


def test_added_unverified_annotation_is_protected(seed_input):
    _, _, output, argv = seed_input
    main(argv)
    path = output/'delivery/A00/images/new_human.json'; path.write_text('{"human_reviewed":true}')
    with pytest.raises(ValueError, match='Unverified files'):
        main(argv+['--stage', 'package'])
    assert path.read_text() == '{"human_reviewed":true}'
