import importlib.util
import json
from pathlib import Path
from types import SimpleNamespace

import numpy as np
import pytest

spec = importlib.util.spec_from_file_location(
    'proposal_stability', Path(__file__).parents[1] / 'scripts/compare_proposal_stability.py')
stability = importlib.util.module_from_spec(spec)
spec.loader.exec_module(stability)


def box(xyxy, score=.9):
    return {'xyxy': xyxy, 'score': score}


def test_crowded_matching_prioritizes_cardinality():
    # Greedy or pure-IoU maximization would pick .99 and discard the second box.
    pairs = stability.match_ious(np.array([[.99, .51], [.51, .49]]))
    assert {(a, b) for a, b, _ in pairs} == {(0, 1), (1, 0)}
    assert stability.match_ious(np.array([[.49]])) == []


def test_box_order_and_small_float_noise_are_not_changes():
    boxes = [box([0, 0, 20, 20]), box([25, 0, 40, 20])]
    result = stability.compare_frame(boxes, list(reversed(boxes)), 100, 100)
    assert result['changed'] == 0 and result['matched'] == 2
    result = stability.compare_frame([boxes[0]], [box([.01, .01, 20.01, 20.01])], 100, 100)
    assert result['changed'] == 0


def test_added_deleted_and_same_count_replacement():
    original = [box([0, 0, 20, 20])]
    result = stability.compare_frame(original, [], 100, 100)
    assert result['deleted'] == 1 and result['presence_changed'] == 1
    result = stability.compare_frame(original, [box([50, 50, 70, 70])], 100, 100)
    assert result['count_changed'] == 0 and result['changed'] == 1
    assert result['added'] == result['deleted'] == 1


def test_empty_agreement_is_separate_and_not_correctness():
    row = stability.compare_frame([], [], 100, 100)
    row.update(group_id='g')
    metrics = stability.aggregate([row], bootstrap=True)
    assert metrics['both_empty'] == 1
    assert metrics['changed_nonempty_frame_rate'] is None
    assert metrics['box_set_jaccard'] is None
    assert stability.judgement([], False)['stable'] is None


def test_threshold_effect_separated_from_geometry():
    old = [box([0, 0, 20, 20], .9)]
    new = old + [box([50, 50, 70, 70], .2)]
    assert stability.compare_frame(old, new, 100, 100)['changed'] == 1
    assert stability.compare_frame(old, new, 100, 100, .7)['changed'] == 0


def test_boundary_shift_and_iou_sensitivity():
    result = stability.compare_frame([box([0, 0, 40, 40])], [box([1, 0, 41, 40])], 100, 100)
    assert result['changed'] == 0  # IoU=.9512 and edge shift=1%.
    result = stability.compare_frame([box([0, 0, 40, 40])], [box([2, 0, 42, 40])], 100, 100)
    assert result['changed'] == 1 and result['changed_iou90'] == 0
    result = stability.compare_frame([box([0, 0, 90, 90])], [box([3, 0, 93, 90])], 100, 100)
    assert result['changed_iou90'] == 1  # 3% edge movement overrides high IoU.


@pytest.fixture
def task(tmp_path):
    protocol = tmp_path / 'protocol'
    protocol.mkdir()
    stability.write(protocol / 'protocol.json', {'version': 'fixture'})
    protocol_sha = stability.digest(protocol / 'protocol.json')
    source = tmp_path / 'source'
    source.mkdir()
    rows = [{'frame_id': f'f{i}', 'group_id': f'g{i}', 'split': 'train', 'domain': 'd',
             'size': [100, 100], 'source_anchor_hash': str(i),
             'image': f'images/train/{stability.slug(f"f{i}")}.png',
             'annotation': f'images/train/{stability.slug(f"f{i}")}.json'} for i in range(5)]
    (source / 'frames.jsonl').write_text(''.join(json.dumps(row) + '\n' for row in rows))
    stability.write(source / 'manifest.json', {'fixture': True})
    paths = []
    for j in range(3):
        path = tmp_path / f'r{j}'
        (path / 'inputs').mkdir(parents=True)
        (path / 'inputs/annotations.jsonl').write_text('manual snapshot fixture\n')
        stability.write(path / 'manual_partition.json', {
            'protocol_sha256': protocol_sha, 'all_manual_frame_ids': [f'f{i}' for i in range(j + 1)],
            'train_frame_ids': [f'f{i}' for i in range(j + 1)], 'validation_frame_ids': [],
            'train_groups': [f'g{i}' for i in range(j + 1)], 'validation_groups': [],
            'manual_annotations_sha256': stability.digest(path / 'inputs/annotations.jsonl'),
            'artifact_sha256': {'inputs/annotations.jsonl': stability.digest(path / 'inputs/annotations.jsonl')}})
        (path / 'training').mkdir()
        (path / 'training/best.pt').write_bytes(f'model{j}'.encode())
        checkpoint_sha = stability.digest(path / 'training/best.pt')
        stability.write(path / 'training/run.json', {'batch_size': 16})
        stability.write(path / 'training/completed.json', {
            'best_checkpoint_sha256': checkpoint_sha, 'completed_epochs': 5,
            'run_config_sha256': stability.digest(path / 'training/run.json')})
        stability.write(path / 'confidence_threshold.json', {
            'checkpoint_sha256': checkpoint_sha, 'selected_confidence': .7, 'test_accessed': False})
        binding = {'checkpoint_sha256': checkpoint_sha,
                   'threshold_sha256': stability.digest(path / 'confidence_threshold.json'),
                   'protocol_sha256': protocol_sha,
                   'source_export_manifest_sha256': stability.digest(source / 'manifest.json'),
                   'source_frame_index_sha256': stability.digest(source / 'frames.jsonl'),
                   'manual_partition_sha256': stability.digest(path / 'manual_partition.json')}
        stability.write(path / 'prediction_manifest.json', {
            'binding': binding, 'state': 'complete', 'expected_frames': 4 - j,
            'completed_frames': 4 - j, 'errors': 0, 'checkpoint_sha256': checkpoint_sha,
            'threshold_sha256': binding['threshold_sha256']})
        for row in rows[j + 1:]:
            receipt = {key: row[key] for key in ('frame_id', 'group_id', 'split', 'domain', 'source_anchor_hash')}
            receipt.update(width=100, height=100, binding=binding, checkpoint_sha256=checkpoint_sha,
                           threshold_sha256=binding['threshold_sha256'], human_reviewed=False,
                           proposal_only=True, status='proposal', boxes_xyxy_score=[box([0, 0, 20, 20])])
            stability.write(path / 'predictions' / (stability.slug(row['frame_id']) + '.json'), receipt)
        paths.append(path)
    args = SimpleNamespace(protocol=protocol, source_export=source, manual_output=paths[-1],
                           round=[f'r{i}={path}' for i, path in enumerate(paths)], expected_rounds=3,
                           output=tmp_path / 'comparison')
    return args, paths


def test_cohort_excludes_entire_latest_manual_union_and_locks_input(task):
    args, paths = task
    rounds, index, remaining, binding = stability.prepare(args)
    assert set(remaining) == {'f3', 'f4'}
    assert binding['excluded_manual_frames'] == 3
    for name, path, ids in rounds:
        receipts, provenance = stability.load_round(name, path, ids, index, remaining, binding)
        assert set(receipts) == {'f3', 'f4'}
    (args.source_export / 'manifest.json').write_text('{"changed":true}')
    with pytest.raises(ValueError, match='task input/method changed'):
        stability.prepare(args)


@pytest.mark.parametrize('defect', ['missing', 'identity', 'dimensions', 'bounds', 'binding', 'reviewed'])
def test_receipts_reject_missing_and_incompatible_sources(task, defect):
    args, paths = task
    rounds, index, remaining, binding = stability.prepare(args)
    name, path, ids = rounds[0]
    receipt = path / 'predictions' / (stability.slug('f3') + '.json')
    if defect == 'missing':
        receipt.unlink()
    else:
        row = stability.read(receipt)
        if defect == 'identity':
            row['source_anchor_hash'] = 'wrong'
        elif defect == 'dimensions':
            row['width'] = 99
        elif defect == 'bounds':
            row['boxes_xyxy_score'][0]['xyxy'][2] = 101
        elif defect == 'binding':
            row['binding']['checkpoint_sha256'] = 'wrong'
        elif defect == 'reviewed':
            row['human_reviewed'] = True
        stability.write(receipt, row)
    with pytest.raises(ValueError):
        stability.load_round(name, path, ids, index, remaining, binding)


def test_group_bootstrap_is_reproducible_and_preserves_group_weights():
    rows = []
    for i in range(4):
        for _ in range(10 if i == 0 else 1):
            row = stability.compare_frame([box([0, 0, 20, 20])],
                                          [] if i == 0 else [box([0, 0, 20, 20])], 100, 100)
            row.update(group_id=f'g{i}')
            rows.append(row)
    a, b = stability.aggregate(rows, True), stability.aggregate(rows, True)
    assert a == b
    assert a['changed_frame_rate'] == 10 / 13
    assert a['group_bootstrap_95ci']['changed_frame_rate'][0] == 0


def test_full_three_round_analysis_has_consensus_and_no_label_writes(task):
    args, _ = task
    rounds, index, remaining, binding = stability.prepare(args)
    original = {path: stability.digest(path) for _, directory, _ in rounds
                for path in (directory / 'inputs').glob('*')}
    result = stability.analysis(args, rounds, index, remaining, binding)
    assert result['state'] == 'complete' and result['judgement']['stable'] is True
    assert result['judgement']['correctness_confirmed'] is False
    assert result['three_round_consensus']['no_substantive_pairwise_change_frames'] == 2
    assert len(result['pairs']) == 3
    assert result['test_ground_truth_accessed'] is False
    assert (args.output / 'review_priority.csv').exists()
    assert all(stability.digest(path) == sha for path, sha in original.items())


def test_partial_result_never_claims_three_round_convergence(task):
    args, _ = task
    args.round = args.round[:2]
    rounds, index, remaining, binding = stability.prepare(args)
    result = stability.analysis(args, rounds, index, remaining, binding)
    assert result['state'] == 'partial'
    assert result['judgement']['stable'] is None
    assert result['judgement']['state'] == 'pending_third_round'


def test_final_delivery_rejects_manual_overlap(task):
    args, _ = task
    rounds, index, remaining, binding = stability.prepare(args)
    result = stability.analysis(args, rounds, index, remaining, binding)
    latest = rounds[-1][1]
    stability.write(latest / 'package_audit.json', {
        'state': 'passed', 'zip_crc': 'passed', 'frames': 2, 'manual_overlap': 1})
    with pytest.raises(ValueError, match='failed audit'):
        stability.final_delivery(args, result, remaining)


def test_final_delivery_with_relative_output_updates_logs_once(task, monkeypatch):
    args, _ = task
    rounds, index, remaining, binding = stability.prepare(args)
    result = stability.analysis(args, rounds, index, remaining, binding)
    project = args.output.parent
    (project / 'docs').mkdir()
    for filename in ('README.md', 'docs/training_log.md', 'docs/change_log.md', 'docs/session_summary.md'):
        (project / filename).write_text('Original log\n')
    monkeypatch.chdir(project)
    monkeypatch.setattr(stability, '__file__', str(project / 'scripts/compare_proposal_stability.py'))
    args.output = Path('comparison')  # Actual workflow uses a relative --output.
    latest = rounds[-1][1]
    archive = latest / 'human_correction_remaining_2_v1.zip'
    archive.write_bytes(b'fixture archive, already audited upstream')
    stability.write(latest / 'package_audit.json', {
        'state': 'passed', 'zip_crc': 'passed', 'frames': 2, 'manual_overlap': 0,
        'split_counts': {'train': 2}, 'zip_sha256': stability.digest(archive),
        'validation_test_labels_scored': False})
    first = stability.final_delivery(args, result, remaining)
    assert first['state'] == 'complete'
    assert Path(first['stability_report']).is_absolute()
    before = (project / 'docs/session_summary.md').read_text()
    stability.final_delivery(args, result, remaining)
    assert (project / 'docs/session_summary.md').read_text() == before
    assert 'comparison/report_zh.md' in before
