"""Publication-10 contracts; all mutation fixtures are isolated/in-memory."""
import copy
import itertools
import json
import sqlite3

import numpy as np
import pytest
from PIL import Image

from pallet import mask_remaining_expand as expand
from pallet import mask_remaining_classes as remaining
from pallet.mask_template_transfer import rectangle
from pallet.pose_review_geometry import raster_mask


QUAD = [[1.1, 1.5], [20, 1], [20.1, 9], [1, 10]]
POLYGON = [[1, 1], [20, 1], [20, 10], [15, 10], [15, 5], [5, 5], [5, 10], [1, 10]]


def row(key, cls=3, frame=None, group=None):
    return dict(target_key=key, target_id='target-'+key, frame_id=frame or 'frame-'+key,
        final_class_id=cls, split='train', group_id=group or 'group-'+key, box_index=0,
        policy_id='camera', viewpoint_candidate='front', source_pcd_sha256='pcd',
        bbox=dict(x=1, y=1, width=19, height=9), size=[24, 16])


def database():
    db = sqlite3.connect(':memory:')
    db.executescript('CREATE TABLE annotations(target_key TEXT,revision INTEGER,payload TEXT);'
        'CREATE TABLE events(id INTEGER PRIMARY KEY,target_key TEXT,revision INTEGER,payload TEXT);')
    return db


def save(db, r, polygon=POLYGON):
    human = dict(target_key=r['target_key'], revision=6, state='draft', source=copy.deepcopy(r),
        binding='binding', polygon=copy.deepcopy(polygon), **dict.fromkeys(expand.FALSE_FLAGS, False))
    raw = json.dumps(human, indent=2)
    db.execute('INSERT INTO annotations VALUES (?,?,?)', (r['target_key'], 6, raw))
    db.execute('INSERT INTO events(target_key,revision,payload) VALUES (?,?,?)', (r['target_key'], 6, raw))
    return remaining.validated_manual_source(db, r['target_key'], r, 'snapshot', 'binding')


def proposal(key, cls=3, frame=None, group=None, quality=.9):
    r = row(key, cls, frame, group)
    return dict(target_key=key, class_id=cls, source=r, metrics=dict(boundary_ncc=quality, ecc=quality))


def request(r):
    return dict(target_key=r['target_key'], class_id=r['final_class_id'],
        source=dict(r, formal_manifest_sha256='formal'), pose_annotation_url='https://example.invalid/'+r['target_key'])


def test_publication_constants_and_no_frozen_engine_mutation():
    assert expand.PUBLICATION_VERSION == 10
    assert expand.ALL_CLASSES == tuple(range(1, 23))
    assert expand.CLASSES == remaining.CLASSES
    assert expand.GENERATION_NAMESPACE == 'remaining_expand_generation'
    assert expand.OUTPUT.name == 'pallet_mask_remaining_expand_v1'
    assert expand.TARGET_PER_CLASS == 100
    assert expand.attempt_target is expand.original.attempt_target
    assert expand.original.CLASSES == (1, 2, 9, 10, 15)
    assert expand.prior.GENERATION_NAMESPACE == 'quota100_continue_generation'
    assert len(expand.HISTORICAL_NAMESPACES) == 7


@pytest.mark.parametrize('cls', expand.CLASSES)
@pytest.mark.parametrize('polygon', [QUAD, POLYGON])
def test_completed_request_counts_all17_exact_humans_without_five_class_filter(cls, polygon):
    r = row('manual', cls)
    with database() as db:
        candidate = save(db, r, polygon)
        before = db.total_changes
        values = expand.completed_manual_proposals([request(r)], {'manual': candidate}, {'manual': r}, [],
            'snapshot', 'formal', 'classification')
        assert db.total_changes == before
    p = values[0]
    assert p['schema'] == expand.MANUAL_SCHEMA
    assert p['polygon'] == polygon and p['source'] == r
    assert p['vertex_count'] == len(polygon)
    assert p['manual_four_point_source'] == (len(polygon) == 4)
    assert p['propagation_seed_eligible'] == (len(polygon) > 4)
    assert ('human_source' in p) == (len(polygon) == 4)
    assert ('template' in p) == (len(polygon) > 4)
    assert p['manual_source_kind'] == 'completed_manual_request'
    assert p['manual_request_sha256'] == expand.sha(request(r))
    assert p['metrics'] == {} and not any(p[f] for f in expand.FALSE_FLAGS)
    assert not p['machine_transfer_performed'] and not p['new_human_edit_created']
    assert not p['pose_store_written'] and not p['gallery_store_written']
    assert p['rendering_id'].startswith(expand.GENERATION_NAMESPACE+':')


@pytest.mark.parametrize('attack', ['duplicate_frame', 'locked_key', 'quarantine', 'gallery_history',
    'missing_human', 'wrong_class', 'wrong_split', 'wrong_formal', 'old_five'])
def test_completed_requests_fail_closed(attack):
    r = row('manual')
    with database() as db:
        candidate = save(db, r)
    req = request(r); previous = []; quarantine = []; gallery = []
    candidates = {'manual': candidate}
    if attack == 'duplicate_frame':
        previous = [proposal('old', 22, r['frame_id'])]
    elif attack == 'locked_key':
        previous = [proposal('manual')]
    elif attack == 'quarantine':
        quarantine = ['manual']
    elif attack == 'gallery_history':
        gallery = ['manual']
    elif attack == 'missing_human':
        candidates = {}
    elif attack == 'wrong_class':
        req['class_id'] = 19
    elif attack == 'wrong_split':
        req['source']['split'] = 'test'
    elif attack == 'wrong_formal':
        req['source']['formal_manifest_sha256'] = 'changed'
    elif attack == 'old_five':
        r['final_class_id'] = 1
    with pytest.raises(ValueError):
        expand.completed_manual_proposals([req], candidates, {'manual': r}, previous,
            'snapshot', 'formal', 'classification', quarantine, gallery)


def test_generation_templates_only_explicit_counted_more_than_four_remaining_humans():
    rows = {r['target_key']: r for r in [row('quad', 4), row('fine', 3),
        row('unselected-sixth', 7), row('old-five', 1)]}
    with database() as db:
        candidates = {k: save(db, r, QUAD if k == 'quad' else POLYGON) for k, r in rows.items()}
    templates = expand.generation_templates(candidates, {'quad', 'fine'}, 'formal', 'classification')
    assert set(templates) == {'fine'}
    t = templates['fine']
    assert t['origin'] == expand.prior.POSE_ORIGIN
    assert t['polygon'] == POLYGON and t['pose_review']['snapshot_sha256'] == 'snapshot'
    assert t['rendering_id'] == 'remaining_expand_generation:fine'
    with pytest.raises(ValueError, match='Old five'):
        expand.generation_templates(candidates, {'old-five'}, 'formal', 'classification')


@pytest.mark.parametrize('historical', ['modified', 'deleted', 'deferred', 'confirmed', 'robot', 'gt'])
def test_any_mask_or_state_history_protected_even_when_restored(historical):
    r = row('restored')
    with database() as db:
        old = dict(target_key='restored', polygon=rectangle(r), state='draft')
        if historical == 'modified':
            old['polygon'] = QUAD
        elif historical in ('deleted', 'deferred', 'confirmed'):
            old['state'] = historical
        else:
            old['robot_command_eligible' if historical == 'robot' else 'ground_truth_eligible'] = True
        db.execute('INSERT INTO events(target_key,revision,payload) VALUES (?,?,?)',
            ('restored', 1, json.dumps(old)))
        current = {'restored': dict(polygon=rectangle(r), state='draft')}
        assert expand.protected_history(db, {'restored': r}, current) == {'restored'}


def test_rectangle_only_pose_save_is_eligible_but_cyclic_box_does_not_create_human_source():
    r = row('pose-only')
    with database() as db:
        current = {'pose-only': dict(polygon=rectangle(r)[1:]+rectangle(r)[:1], state='draft', pose={'x': 1})}
        assert expand.protected_history(db, {'pose-only': r}, current) == set()


def test_locked141_cannot_be_reallocated_to_scarcer_class_even_to_increase_total():
    locked = [proposal('human141', 12, 'shared')]
    candidates = [proposal('scarce', 19, 'shared'), proposal('common', 12)]
    selected, dispositions, assignments, _ = expand.select_locked_diverse(locked, candidates, goal=1)
    assert selected == []
    assert assignments == [] and locked[0]['target_key'] == 'human141'
    assert dispositions == {'scarce': 'accepted_duplicate_frame_not_selected',
        'common': 'accepted_quota_full_not_selected'}


def test_augmenting_matching_avoids_greedy_crossclass_frame_loss():
    candidates = [proposal('c3-shared', 3, 'shared'), proposal('c3-free', 3, 'free'),
        proposal('c19-shared', 19, 'shared'), proposal('c19-second', 19, 'second'),
        proposal('c17-second', 17, 'second')]
    selected, _, assignments, order = expand.select_locked_diverse([], candidates, goal=1)
    assert len(selected) == 3
    assert {p['class_id'] for p in selected} == {3, 17, 19}
    assert len({v['frame_id'] for v in assignments}) == 3
    assert order.index(17) < order.index(3)
    assert expand.select_locked_diverse([], list(reversed(candidates)), goal=1)[0] == selected


def test_matching_cardinality_equals_exhaustive_solution_for_small_graphs():
    edges = [(c, f) for c in (3, 17, 19) for f in ('a', 'b', 'c')]
    for mask in range(1 << len(edges)):
        candidates = [proposal(f'{c}-{f}', c, f) for i, (c, f) in enumerate(edges) if mask & (1 << i)]
        selected = expand.select_locked_diverse([], candidates, goal=1)[0]
        maximum = 0
        for n in range(min(3, len(candidates))+1):
            for choice in itertools.combinations(candidates, n):
                if len({p['class_id'] for p in choice}) == n and len({p['source']['frame_id'] for p in choice}) == n:
                    maximum = n
        assert len(selected) == maximum


def test_quality_then_diversity_under_quota100_and_sameframe_best_representative():
    locked = [proposal('old', 3, group='overrepresented')]
    candidates = [proposal('low', 3, 'same', 'new-group', .8),
        proposal('high', 3, 'same', 'new-group', .98),
        proposal('excellent-common', 3, group='overrepresented', quality=.99)]
    selected, disposition, _, _ = expand.select_locked_diverse(locked, candidates, goal=2)
    assert [p['target_key'] for p in selected] == ['high']
    assert disposition['low'] == 'accepted_duplicate_frame_not_selected'
    assert disposition['excellent-common'] == 'accepted_quota_full_not_selected'
    scarce = [proposal(str(i), 19) for i in range(90)]
    assert len(expand.select_locked_diverse([], scarce)[0]) == 90
    assert len(expand.select_locked_diverse([], scarce+[proposal('extra'+str(i), 19) for i in range(20)])[0]) == 100


def test_class19_prior90_capacity_becomes88_without_losing_previously_counted_humans():
    baseline = [proposal('baseline'+str(i), 2, 'frame-'+str(i)) for i in range(4)]
    humans = [proposal('reused20', 20, 'frame-4'), proposal('completed3', 3, 'frame-5')]
    # Original 96 frames, two independent protected-history frames (6 and 7).
    candidates = [proposal('c19-'+str(i), 19, 'frame-'+str(i)) for i in range(96) if i not in (6, 7)]
    assert len(expand.select_locked_diverse(baseline, candidates)[0]) == 90
    selected = expand.select_locked_diverse(baseline+humans, candidates)[0]
    assert len(selected) == 88
    assert not {p['source']['frame_id'] for p in humans} & {p['source']['frame_id'] for p in selected}


def test_target_inventory_all_targets_and_protected_frames_not_just_keys():
    rs = [row('locked', 3, 'same'), row('crossclass', 19, 'same'), row('gallery'), row('masked'),
        row('quarantine'), row('eligible'), row('oldclass', 1)]
    rows = {r['target_key']: r for r in rs}
    work, outcomes = expand.target_inventory(rows, [proposal('locked', 3, 'same')],
        dict.fromkeys(rows, 'eligible'), {'masked'}, {'gallery'}, {'quarantine'})
    assert [k for k, _ in work] == ['eligible']
    reasons = {v['target_key']: v['reason'] for v in outcomes}
    assert reasons['crossclass'] == 'locked_counted_frame'
    assert reasons['masked'] == 'pose_mask_or_state_history_protected'
    assert reasons['oldclass'] == 'class_not_requested'
    assert len(work)+len(outcomes) == len(rows)


def test_quad_and_machine_drawable_flags_masks_and_no_synthetic_human_scores(tmp_path):
    r = row('quad', 4)
    with database() as db:
        candidate = save(db, r, QUAD)
    manual = expand.completed_manual_proposals([request(r)], {'quad': candidate}, {'quad': r}, [],
        'snapshot', 'formal', 'classification')
    class Evidence:
        def image(self, frame):
            return np.zeros((16, 24), np.uint8)
    for d in ('cases', 'images', 'masks', 'labelme'):
        (tmp_path/d).mkdir()
    expand.prior.package_manual_preview(tmp_path, manual, Evidence())
    label = expand.read_json(tmp_path/'labelme/quad.json')
    assert label['shapes'][0]['points'] == QUAD
    assert label['mask_template_provenance']['metrics'] == {}
    assert not label['flags']['machine_draft'] and not label['flags']['ground_truth_eligible']
    assert np.array_equal(np.asarray(Image.open(tmp_path/'masks/quad.png')) > 0, raster_mask(QUAD, 24, 16))


def test_current_snapshot_counts_not_parent_report_counts():
    previous = [proposal('old', 1), proposal('saved-now', 1)]
    reused = [proposal('reuse', 22)]
    completed = [proposal('complete', 3)]
    new = [proposal('machine', 3)]
    reviews = {'saved-now': dict(human_edited=True, event_kind='human_mask_gallery_revision',
        state='draft', polygon=POLYGON)}
    records = [dict(target_key=k, manual_four_point_source=True) for k in ('reuse', 'complete')]
    disposition = [dict(target_key='old')]
    rows = {p['target_key']: p['source'] for p in previous+reused+completed+new}
    counts = expand.class_counts(previous, reused, completed, new, reviews, records, disposition,
        {}, rows, [], [], dict.fromkeys(expand.ALL_CLASSES, 100))
    assert counts[1]['saved_human'] == 1 and counts[1]['user_reviewed_no_change'] == 1
    assert counts[3]['saved_human'] == 1 and counts[3]['new_candidates'] == 1
    assert counts[3]['total'] == 2 and counts[3]['manual_four_point_sources'] == 1
    assert counts[22]['manual_existing_reused'] == 1


def test_report_all22_filter_default_only_if_new_and_horizontal_containment(tmp_path):
    counts = {c: dict.fromkeys(('old_total', 'manual_existing_reused', 'manual_requested',
        'saved_human', 'new_candidates', 'total', 'missing', 'saved_pose_human',
        'saved_gallery_human', 'manual_four_point_sources', 'capacity_matching_distinct_frames'), 0) for c in expand.ALL_CLASSES}
    expand.build_report(tmp_path, [], counts, set(), set(), set(), set(), {}, set(), {})
    text = (tmp_path/'report_zh.html').read_text()
    assert 'id="only-new" checked' not in text
    assert all(f'<option value="{c}">{c}</option>' in text for c in expand.ALL_CLASSES)
    assert 'max-width:1090px' in text and 'overflow-x:auto' in text
    assert '本轮新增机器候选 0 份' in text
    expand.build_report(tmp_path, [], counts, {'new'}, set(), set(), set(), {}, set(), {})
    assert 'id="only-new" checked' in (tmp_path/'report_zh.html').read_text()
    assert expand.read_json(tmp_path/'font_coverage.json')['all_report_Chinese_present']


def test_generation_refuses_overwrite_outside_outputs_and_invalid_workers(tmp_path):
    existing = tmp_path/'existing'; existing.mkdir()
    marker = existing/'user.txt'; marker.write_text('keep')
    with pytest.raises(ValueError, match='new outputs directory'):
        expand.generate(existing)
    assert marker.read_text() == 'keep'
    with pytest.raises(ValueError, match='new outputs directory'):
        expand.generate(tmp_path/'outside')
    with pytest.raises(ValueError, match='Workers'):
        expand.generate(expand.PROJECT/'outputs/nonexistent-invalid-workers-expand-test', workers=0)
