"""Counting must not promote quality; dataset updates preserve holdout isolation."""
from collections import Counter
from contextlib import closing
from copy import deepcopy
import json
from pathlib import Path
import sqlite3

import pytest

from pallet import mask_dataset_update as update
from pallet.mask_template_refined import snapshot_database


def manual(key, cls, frame=None, image=None, isolated=False):
    return dict(target_key=key, row=dict(frame_id=frame or key, final_class_id=cls,
        image=image or key+'.png', size=[100, 80]), polygon=[[10, 10], [60, 10], [60, 50], [10, 50]],
        **({'source_quality': 'nonconforming'} if isolated else {}))


def test_raw_fifty_does_not_promote_seven_isolated_labels(monkeypatch):
    monkeypatch.setattr(update, 'digest', lambda path: str(path))
    regular = [manual(f'{c}-{i}', c) for c in range(1, 23) for i in range(43 if c == 19 else 50)]
    isolated = [manual(f'nc-{i}', 19, isolated=True) for i in range(7)]
    value = update.coverage(regular, isolated, Path('/nonexistent-test-formal'))
    assert value['classes'][19]['all_manual_contours'] == 50
    assert value['classes'][19]['standard_saved'] == 43
    assert value['classes'][19]['missing_to50_excluding_nonconforming'] == 7
    assert value['all22_reach50_including_nonconforming'] is True
    assert value['all22_reach50_excluding_nonconforming'] is False
    assert value['source_flags_or_qualification_changed'] is False


@pytest.mark.parametrize('duplicate', ['key', 'frame', 'image_bytes'])
def test_global_duplicate_never_increases_manual_count(monkeypatch, duplicate):
    monkeypatch.setattr(update, 'digest', lambda path: str(path))
    one = manual('one', 19); two = manual('two', 19, isolated=True)
    if duplicate == 'key':
        two['target_key'] = one['target_key']
    elif duplicate == 'frame':
        two['row']['frame_id'] = one['row']['frame_id']
    else:
        two['row']['image'] = one['row']['image']
    with pytest.raises(ValueError, match='Duplicate image'):
        update.coverage([one], [two], Path('/test'))


def test_empty_check_record_is_not_a_manual_contour(monkeypatch):
    monkeypatch.setattr(update, 'digest', lambda path: str(path))
    empty = manual('empty', 19, isolated=True); empty['polygon'] = []
    with pytest.raises(ValueError):
        update.coverage([], [empty], Path('/test'))


@pytest.mark.parametrize('prior', ['train', 'dev', 'calibration'])
def test_existing_group_assignments_never_move(prior):
    assert update.assign_split('old', {'old': prior}) == prior


def test_new_groups_enter_train_not_a_posthoc_new_blind_validation():
    assert update.assign_split('new', {'old': 'dev'}) == 'train'


@pytest.mark.parametrize('split', ['val', 'test'])
def test_formal_holdout_rejected_before_polygon_or_eligibility_access(monkeypatch, split):
    def forbidden(*args):
        raise AssertionError('Heldout label reached eligibility')
    monkeypatch.setattr(update, 'eligibility', forbidden)
    assert update.gate({}, {'split': split}, [], 'g', set()) == (['formal_val_test_label_forbidden'], None, [])


def test_nonconforming_train_requires_clearance_not_just_a_real_polygon(monkeypatch):
    def forbidden(*args):
        raise AssertionError('Uncleared isolated label reached supervision')
    monkeypatch.setattr(update, 'eligibility', forbidden)
    assert update.gate({'source_quality': 'nonconforming'}, {'split': 'train'}, [], 'g', set(), newly_reviewable=True) == (
        ['nonconforming_source_pending_quality_clearance'], None, [])


def test_visual_review_does_not_waive_leakage_neighbors_or_class14(monkeypatch):
    reasons = ['unknown_or_narrow_observation_surface', 'unknown_or_edge_only_structure',
        'global_component_linked_to_formal_val_test', 'known_neighbor_bbox_intersects_context_roi', 'class14_long_short_surface_ambiguous']
    monkeypatch.setattr(update, 'eligibility', lambda *args: (reasons, [1, 2, 3, 4]))
    hard, roi, notes = update.gate({}, {'split': 'train'}, [], 'g', set(), newly_reviewable=True)
    assert hard == reasons[2:] and notes == reasons[:2]
    assert roi == [1, 2, 3, 4]


def test_no_visual_authorization_leaves_unknown_semantics_hard(monkeypatch):
    monkeypatch.setattr(update, 'eligibility', lambda *args: (['unknown_or_narrow_observation_surface'], [1, 2, 3, 4]))
    assert update.gate({}, {'split': 'train'}, [], 'g', set())[0] == ['unknown_or_narrow_observation_surface']


def test_existing_dataset_output_cannot_be_prepared_again(tmp_path):
    with pytest.raises(ValueError, match='Fresh dataset'):
        update.prepare(tmp_path)


def test_frozen_dataset_cannot_be_overwritten(tmp_path):
    (tmp_path/'dataset_manifest.json').write_text('{}')
    with pytest.raises(ValueError, match='replace a frozen'):
        update.freeze(tmp_path/'unused.json', tmp_path)


@pytest.fixture
def isolated_snapshot(tmp_path, monkeypatch):
    # Actual event bytes are copied transactionally, never modified in production.
    path = tmp_path/'isolated.sqlite3'; snapshot_database(update.EXCEPTION_STORE, path)
    proposals = update.jsonl(update.EXCEPTIONS/'proposals.jsonl')
    rows = {p['target_key']: p['source'] for p in proposals}
    image_shas = {Path('/test-formal')/p['source']['image']: p['image_sha256'] for p in proposals}
    original = update.digest
    monkeypatch.setattr(update, 'digest', lambda p: image_shas[Path(p)] if Path(p) in image_shas else original(p))
    return path, rows


def test_isolated_raw_latest_and_all_events_bind_exactly(isolated_snapshot):
    path, rows = isolated_snapshot
    records, review_only, events = update.exception_snapshot(path, rows, Path('/test-formal'))
    assert len(records) == 7 and not review_only and len(events) >= 7
    assert Counter(r['row']['split'] for r in records) == {'train': 2, 'val': 1, 'test': 4}
    assert all(r['source_quality'] == 'nonconforming' and r['qualification'] == 'pending_quality_clearance' for r in records)
    assert all(r['counts_toward_under50'] is False for r in records)


@pytest.mark.parametrize('field,value', [('worklist_manifest_sha256', 'wrong'), ('proposal_publication', 15),
    ('base_proposal_id', 'wrong'), ('ground_truth_eligible', True), ('counts_toward_under50', True),
    ('event_kind', 'human_mask_gallery_revision'), ('source_quality', 'qualified')])
def test_tampered_isolated_source_or_promotion_rejected(isolated_snapshot, field, value):
    path, rows = isolated_snapshot
    with closing(sqlite3.connect(path)) as db:
        event_id, raw = db.execute('SELECT id,payload FROM events ORDER BY id LIMIT 1').fetchone()
        payload = json.loads(raw); payload[field] = value
        db.execute('UPDATE events SET payload=? WHERE id=?', (json.dumps(payload), event_id)); db.commit()
    with pytest.raises(ValueError, match='binding or unauthorized'):
        update.exception_snapshot(path, rows, Path('/test-formal'))


def test_non_latest_current_is_not_counted(isolated_snapshot):
    path, rows = isolated_snapshot
    with closing(sqlite3.connect(path)) as db:
        key, raw = db.execute('SELECT target_key,payload FROM reviews LIMIT 1').fetchone()
        payload = json.loads(raw); payload['note'] = 'not an actual event'
        db.execute('UPDATE reviews SET payload=? WHERE target_key=?', (json.dumps(payload), key)); db.commit()
    with pytest.raises(ValueError, match='raw latest'):
        update.exception_snapshot(path, rows, Path('/test-formal'))
