import json
from dataclasses import replace
from types import SimpleNamespace

import pytest

from pallet.annotation import AnnotationSet, Box, FrameAnnotation, Provenance, Review, record_review, save_jsonl
from pallet.annotation import cycle


def test_candidates_use_frozen_metadata_and_exclude_entire_target_group():
    rows = [SimpleNamespace(frame_id=str(i), group_id=group, domain=domain, source_sha256='raw')
            for i, (group, domain) in enumerate([
                ('source', 'source'), ('audit', 'source'), ('held', 'target'), ('held', 'source')])]
    assignments = {'source': 'train', 'audit': 'train', 'held': 'train'}
    labels = SimpleNamespace(frames={r.frame_id: SimpleNamespace(
        status='unreviewed', split='train', group_id=r.group_id, source_anchor_hash='raw') for r in rows})
    assert [r.frame_id for r in cycle.eligible_source_frames(rows, labels,
        audit_groups=['audit'], target_domain='target', assignments=assignments)] == ['0']
    assert cycle.eligible_source_frames(rows, labels, audit_groups=['audit'],
        target_domain='target', assignments=assignments, excluded_frame_ids=['0']) == []
    labels.frames['0'].split = 'test'
    with pytest.raises(ValueError, match='frozen split/group/anchor'):
        cycle.eligible_source_frames(rows, labels, assignments=assignments)
    with pytest.raises(ValueError, match='frozen assignments'):
        cycle.eligible_source_frames(rows, labels)


def test_reviewed_exclusion_requires_independent_source_training_labels(tmp_path):
    rows = [SimpleNamespace(frame_id=fid, group_id=group, domain='source',
                            source_sha256=f'raw-{fid}')
            for fid, group in [('train', 'source'), ('test', 'held')]]
    assignments = {'source': 'train', 'held': 'test'}
    frames = []
    for row in rows:
        frame = FrameAnnotation(row.frame_id, row.group_id, 'raw.pcd', 10, 10,
                                split=assignments[row.group_id],
                                source_anchor_hash=row.source_sha256)
        record_review(frame, Review(f'{row.frame_id}-a', 'approve', blind=True), status='negative')
        record_review(frame, Review(f'{row.frame_id}-b', 'approve', blind=True))
        frames.append(frame)
    labels = tmp_path/'labels.jsonl'
    save_jsonl(AnnotationSet(frames), labels)
    selection = tmp_path/'selection.json'
    selection.write_text(json.dumps({'train': ['train']}))
    excluded, manifest = cycle.verified_reviewed_exclusions(
        rows, assignments, selection, labels)
    assert excluded == {'train'}
    assert manifest['labels_sha256'] == cycle.file_digest(labels)
    selection.write_text(json.dumps({'train': ['test']}))
    with pytest.raises(ValueError, match='independent source-training'):
        cycle.verified_reviewed_exclusions(rows, assignments, selection, labels)
    selection.write_text(json.dumps({'train': ['train']}))
    frames[0].provenance.append(Provenance('detector'))
    save_jsonl(AnnotationSet(frames), labels)
    with pytest.raises(ValueError, match='independent source-training'):
        cycle.verified_reviewed_exclusions(rows, assignments, selection, labels)
    frames[0].provenance.clear()
    frames[0].reviews.clear()
    save_jsonl(AnnotationSet(frames), labels)
    with pytest.raises(ValueError, match='independent source-training'):
        cycle.verified_reviewed_exclusions(rows, assignments, selection, labels)


def test_bound_exclusion_selection_verifies_source_labels_and_manifest(tmp_path, monkeypatch):
    protocol = tmp_path/'protocol'
    protocol.mkdir()
    (protocol/'protocol.json').write_text('{}')
    row = SimpleNamespace(frame_id='train', group_id='source', domain='source',
                          source_sha256='raw-train')
    frame = FrameAnnotation('train', 'source', 'raw.pcd', 10, 10, split='train',
                             source_anchor_hash='raw-train')
    record_review(frame, Review('a', 'approve', blind=True), status='pos',
                  boxes=[Box(1, 1, 4, 4)])
    record_review(frame, Review('b', 'approve', blind=True))
    labels = tmp_path/'labels.jsonl'
    save_jsonl(AnnotationSet([frame]), labels)
    monkeypatch.setattr(cycle, 'load_protocol', lambda _: ([row], {'source': 'train'}, {}))

    selection = tmp_path/'bound.json'
    bound = cycle.bind_exclusion_selection(protocol, labels, selection)
    assert bound['frames'] == 1
    excluded, manifest = cycle.verified_reviewed_exclusions(
        [row], {'source': 'train'}, selection, labels,
        protocol_sha256=cycle.file_digest(protocol/'protocol.json'))
    assert excluded == {'train'}
    assert manifest['selection_manifest_sha256'] == cycle.file_digest(
        selection.with_suffix('.manifest.json'))

    sidecar = selection.with_suffix('.manifest.json')
    modified = json.loads(sidecar.read_text())
    modified['protocol_sha256'] = 'different-protocol'
    sidecar.write_text(json.dumps(modified))
    with pytest.raises(ValueError, match='provenance manifest mismatch'):
        cycle.verified_reviewed_exclusions(
            [row], {'source': 'train'}, selection, labels,
            protocol_sha256=cycle.file_digest(protocol/'protocol.json'))


def test_preselection_is_deterministic_and_cannot_be_rebound(tmp_path, monkeypatch):
    protocol = tmp_path/'protocol'
    protocol.mkdir()
    (protocol/'protocol.json').write_text('{}')
    annotations = tmp_path/'labels.jsonl'
    annotations.write_text('frozen labels')
    rows = [SimpleNamespace(frame_id=fid, group_id=group, domain='source',
                            source_sha256=f'raw-{fid}')
            for fid, group in [('a', 'source'), ('b', 'source'),
                               ('c', 'source'), ('audit', 'audit')]]
    assignments = {'source': 'train', 'audit': 'train'}
    labels = SimpleNamespace(frames={row.frame_id: SimpleNamespace(
        status='unreviewed', split='train', group_id=row.group_id,
        source_anchor_hash=row.source_sha256) for row in rows})
    monkeypatch.setattr(cycle, 'load_protocol', lambda _: (rows, assignments, {}))
    monkeypatch.setattr(cycle, 'load_jsonl', lambda _: labels)
    output = tmp_path/'selection.json'
    first = cycle.preselect_round(protocol, annotations, output,
                                  audit_groups=['audit'], limit=2)
    assert first['selected_frames'] == 2
    assert cycle.preselect_round(protocol, annotations, output,
                                 audit_groups=['audit'], limit=2) == first
    selected = json.loads(output.read_text())['train']
    assert len(selected) == 2 and 'audit' not in selected
    output.write_text(json.dumps({'train': ['audit'], 'val': [], 'test': []}))
    with pytest.raises(ValueError, match='existing preselection differs'):
        cycle.preselect_round(protocol, annotations, output,
                              audit_groups=['audit'], limit=2)


def test_teacher_audit_must_be_source_train_and_exclude_validation_access():
    rows = [SimpleNamespace(group_id='target', domain='target')]
    assignments = {'source': 'train', 'audit': 'train', 'val': 'val', 'target': 'test'}
    run = {'train_groups': ['source'], 'val_groups': ['audit']}
    with pytest.raises(ValueError, match='validation access'):
        cycle._teacher_access(rows, assignments, run, ['audit'], 'target')
    run['val_groups'] = ['val']
    with pytest.raises(ValueError, match='source-training'):
        cycle._teacher_access(rows, assignments, run, ['val'], 'target')
    run['normalization_groups'] = ['audit']
    with pytest.raises(ValueError, match='validation access'):
        cycle._teacher_access(rows, assignments, run, ['audit'], 'target')


def _chain():
    result = []
    for index in range(1, 4):
        row = {'state': 'complete', 'round_id': str(index), 'round_index': index,
               'protocol_sha256': 'protocol', 'audit_groups': ['audit'],
               'audit_frame_ids': ['frame'], 'audit_review_ids': {'frame': [f'{index}-a', f'{index}-b']},
               'input_labels_sha256': str(index - 1), 'reviewed_labels_sha256': str(index),
               'parent_state_sha256': result[-1]['state_sha256'] if result else None,
               'audit_change_rate': 0.0}
        row['state_sha256'] = cycle._state_digest(row)
        result.append(row)
    return result


def test_convergence_requires_unique_continuous_bound_rounds():
    chain = _chain()
    assert cycle.converged(chain)
    assert not cycle.converged([chain[0]] * 3)
    assert not cycle.converged(chain[1:])
    chain[2]['input_labels_sha256'] = 'unrelated'
    chain[2]['state_sha256'] = cycle._state_digest(chain[2])
    assert not cycle.converged(chain)
    chain = _chain()
    chain[2]['audit_review_ids'] = chain[1]['audit_review_ids']
    chain[2]['state_sha256'] = cycle._state_digest(chain[2])
    assert not cycle.converged(chain)


def test_audit_change_uses_one_to_one_box_matching_with_small_jitter_tolerance():
    frame = FrameAnnotation('f', 'g', 'raw.pcd', 256, 256, status='pos',
                            boxes=[Box(10, 10, 80, 40), Box(130, 10, 80, 40)])
    jittered = replace(frame, boxes=[Box(131, 10, 80, 40), Box(11, 10, 80, 40)])
    assert not cycle._audit_changed(frame, jittered)
    duplicated = replace(frame, boxes=[Box(10, 10, 80, 40), Box(11, 10, 80, 40)])
    assert cycle._audit_changed(frame, duplicated)
    missing = replace(frame, boxes=[Box(10, 10, 80, 40)])
    assert cycle._audit_changed(frame, missing)


def test_audit_matching_maximizes_count_above_iou_threshold(monkeypatch):
    frame = FrameAnnotation('f', 'g', 'raw.pcd', 256, 256, status='pos',
                            boxes=[Box(1, 1, 10, 10), Box(21, 1, 10, 10)])
    revised = replace(frame, boxes=[Box(1, 21, 10, 10), Box(21, 21, 10, 10)])
    scores = {(1, 1): .99, (1, 21): .86, (21, 1): .86, (21, 21): .84}
    monkeypatch.setattr(cycle, 'iou_xywh', lambda left, right: scores[(left[0], right[0])])
    assert not cycle._audit_changed(frame, revised, minimum_iou=.85)


def _audit_fixture(tmp_path, monkeypatch):
    protocol = tmp_path / 'protocol'
    protocol.mkdir()
    (protocol / 'protocol.json').write_text('{}')
    frame = FrameAnnotation('audit-frame', 'audit', 'raw.png', 10, 10,
                            split='train', source_anchor_hash='raw')
    record_review(frame, Review('old-a', 'approve'), status='negative')
    record_review(frame, Review('old-b', 'approve'))
    before = tmp_path / 'input.jsonl'
    save_jsonl(AnnotationSet([frame]), before)
    round_dir = tmp_path / 'round'
    round_dir.mkdir()
    save_jsonl(AnnotationSet([frame]), round_dir / 'proposals.jsonl')
    state = {'state': 'awaiting_review', 'protocol': str(protocol),
             'protocol_sha256': cycle.file_digest(protocol/'protocol.json'),
             'input_labels': str(before), 'input_labels_sha256': cycle.file_digest(before),
             'proposal_labels_sha256': cycle.file_digest(round_dir/'proposals.jsonl'),
             'round_id': 'round-1', 'round_index': 1, 'parent_round': None,
             'parent_state_sha256': None, 'audit_groups': ['audit'], 'frame_ids': []}
    cycle.write_json(round_dir/'round.json', state)
    row = SimpleNamespace(frame_id=frame.frame_id, group_id='audit', source_sha256='raw')
    monkeypatch.setattr(cycle, 'load_protocol', lambda path: ([row], {'audit': 'train'}, {}))
    return frame, before, round_dir, state


def _report(path, before, after, state):
    cycle.write_json(path, {'frame_ids': ['audit-frame'], 'round_id': state['round_id'],
        'protocol_sha256': state['protocol_sha256'], 'before_labels': str(before),
        'after_labels': str(after), 'before_labels_sha256': cycle.file_digest(before),
        'after_labels_sha256': cycle.file_digest(after)})


def test_identical_audit_snapshots_cannot_finalize(tmp_path, monkeypatch):
    _, before, round_dir, state = _audit_fixture(tmp_path, monkeypatch)
    report = tmp_path/'audit.json'
    _report(report, before, before, state)
    with pytest.raises(ValueError, match='fresh independent'):
        cycle.finalize_round(round_dir, before, report)


def test_new_independent_reviews_allow_unchanged_payload_and_bind_round(tmp_path, monkeypatch):
    frame, before, round_dir, state = _audit_fixture(tmp_path, monkeypatch)
    record_review(frame, Review('new-a', 'approve'))
    record_review(frame, Review('new-b', 'approve'))
    after = tmp_path/'reviewed.jsonl'
    save_jsonl(AnnotationSet([frame]), after)
    report = tmp_path/'audit.json'
    _report(report, before, after, state)
    done = cycle.finalize_round(round_dir, after, report)
    assert done['audit_change_rate'] == 0.0
    assert len(done['audit_review_ids']['audit-frame']) == 2
    assert done['state_sha256'] == cycle._state_digest(done)


def test_new_ids_from_old_reviewers_do_not_count_as_independent(tmp_path, monkeypatch):
    frame, before, round_dir, state = _audit_fixture(tmp_path, monkeypatch)
    frame.reviews = [replace(review, review_id='new-' + review.review_id) for review in frame.reviews]
    after = tmp_path/'reviewed.jsonl'
    save_jsonl(AnnotationSet([frame]), after)
    report = tmp_path/'audit.json'
    _report(report, before, after, state)
    with pytest.raises(ValueError, match='independent new reviewers'):
        cycle.finalize_round(round_dir, after, report)


def test_audit_cannot_substitute_unrelated_round_output(tmp_path, monkeypatch):
    frame, before, round_dir, state = _audit_fixture(tmp_path, monkeypatch)
    record_review(frame, Review('new-a', 'approve'))
    record_review(frame, Review('new-b', 'approve'))
    after = tmp_path/'reviewed.jsonl'
    save_jsonl(AnnotationSet([frame]), after)
    report = tmp_path/'audit.json'
    _report(report, before, after, state)
    with pytest.raises(ValueError, match='round input and reviewed output'):
        cycle.finalize_round(round_dir, before, report)


def test_parent_state_rejects_modified_completed_round(tmp_path):
    state = _chain()[0]
    path = tmp_path/'round.json'
    cycle.write_json(path, state)
    assert cycle._parent_state(tmp_path)['round_id'] == '1'
    state['reviewed_labels_sha256'] = 'changed'
    cycle.write_json(path, state)
    with pytest.raises(ValueError, match='verified completed'):
        cycle._parent_state(path)
