"""Publication12 contracts; synthetic SQLite fixtures never write production stores."""
import copy
import hashlib
import json
import sqlite3

import numpy as np
import pytest
from PIL import Image

from pallet import mask_remaining_round2 as round2
from pallet.mask_template_transfer import rectangle
from pallet.pose_review_geometry import canonical, 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):
    return dict(target_key=key, target_id='target-'+key, frame_id=frame or 'frame-'+key,
        final_class_id=cls, split='train', group_id='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(path=':memory:'):
    db = sqlite3.connect(path)
    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 payload(r, polygon=None, revision=2):
    return dict(target_key=r['target_key'], revision=revision, state='draft', source=copy.deepcopy(r),
        binding='binding', polygon=copy.deepcopy(rectangle(r) if polygon is None else polygon),
        reviewer='real reviewer 检验', pose=dict(robot_command_eligible=False), **dict.fromkeys(round2.FALSE_FLAGS, False))


def insert(db, value, raw=None):
    raw = json.dumps(value, indent=2, ensure_ascii=False) if raw is None else raw
    db.execute('INSERT INTO annotations VALUES (?,?,?)', (value['target_key'], value['revision'], raw))
    db.execute('INSERT INTO events(target_key,revision,payload) VALUES (?,?,?)',
        (value['target_key'], value['revision'], raw))
    return raw


def statement(key, text=round2.USER_STATEMENT):
    return dict(statement_zh=text, scoped_request_keys=[key], reviewed_original_rectangle_keys=[key])


def proposal(key, cls=3, frame=None):
    return dict(target_key=key, class_id=cls, source=row(key, cls, frame), metrics={})


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


def test_constants_frozen_helpers_and_original_statement_not_monkeypatched():
    old = round2.previous_generation
    assert round2.PUBLICATION_VERSION == 12 and round2.VERSION == 'remaining_saved_review_round2_v1'
    assert round2.GENERATION_NAMESPACE == 'remaining_round2_generation'
    assert round2.OUTPUT.name == 'pallet_mask_remaining_round2_v1'
    assert round2.PARENT.name == 'pallet_mask_remaining_continue_v1'
    assert round2.REQUESTS.name == 'pallet_mask_remaining_followup_requests_v2'
    assert round2.attempt_target is round2.original.attempt_target
    assert round2.original.CLASSES == (1, 2, 9, 10, 15)
    assert len(round2.HISTORICAL_NAMESPACES) == 10
    assert old.PUBLICATION_VERSION == 11 and old.USER_STATEMENT != round2.USER_STATEMENT
    assert old.ORIGINAL_REVIEWS.keys().isdisjoint(round2.ORIGINAL_REVIEWS)
    assert round2.HISTORICAL_NAMESPACES[-1] == old.GENERATION_NAMESPACE


def test_current_statement_scope_does_not_expand_old_authority():
    requests = [request(row('current', 4))]
    s = round2.review_statement(requests, [proposal('candidate')])
    assert s['statement_zh'] == round2.USER_STATEMENT
    assert s['scoped_request_keys'] == ['current'] and s['scoped_candidate_keys'] == ['candidate']
    assert s['candidate_count_in_statement'] == s['actual_parent_batch_saved_count'] == 185
    assert s['old_original_rectangle_statement'] == 'parent_evidence/review_completion_statement.json'
    assert s['old_original_rectangle_statement_scope_unchanged'] and s['old_42_unsaved_not_inferred_reviewed']
    assert not s['creates_human_event'] and not s['authorizes_propagation']


def test_original_review_real_raw_utf8_and_old_statement_binding_stays_old():
    r = row('box', 4); value = payload(r)
    with database() as db:
        raw = insert(db, value); before = db.total_changes
        v, metadata, record = round2.exact_original_rectangle_review(db, 'box', r, 'snapshot', 'binding', statement('box'))
        old_statement = statement('box', 'old exact user statement')
        _, _, old = round2.exact_original_rectangle_review(db, 'box', r, 'snapshot', 'binding', old_statement,
            expected_statement=old_statement['statement_zh'])
        assert db.total_changes == before
        with pytest.raises(ValueError):
            round2.remaining.validated_manual_source(db, 'box', r, 'snapshot', 'binding')
    assert v == value and record['current_payload_raw'] == record['saved_review_event_payload_raw'] == raw
    assert record['saved_review_event_sha256'] == hashlib.sha256(raw.encode('utf-8')).hexdigest()
    assert record['user_statement_sha256'] != old['user_statement_sha256']
    assert old['user_statement_sha256'] == round2.sha(old_statement)
    assert record['saved_pose_review_source'] and record['user_reviewed_no_change']
    assert not record['human_edited'] and not record['propagation_seed_eligible']
    assert not any(record[f] for f in round2.FALSE_FLAGS)
    assert 'human_polygon_revision' not in record and 'human_polygon_revision' not in metadata


@pytest.mark.parametrize('attack', ['target', 'revision', 'binding', 'split', 'bbox', 'frame', 'policy',
    'class', 'viewpoint', 'changed_quad', 'deleted', 'deferred', 'confirmed', 'gt', 'train', 'robot',
    'nested_robot', 'auto', 'machine', 'origin', 'absent_event', 'different_raw', 'newer_event',
    'older_modified', 'older_confirmed', 'older_machine', 'statement', 'scope', 'duplicate_current', 'duplicate_event'])
def test_original_review_rejects_identity_state_history_flags_and_unscoped_authority(attack):
    r = row('box', 4); value = payload(r); s = statement('box')
    if attack in ('target', 'revision', 'binding'):
        value[{'target': 'target_key', 'revision': 'revision', 'binding': 'binding'}[attack]] = 'wrong'
    elif attack in ('split', 'bbox', 'frame', 'policy', 'class', 'viewpoint'):
        f = {'frame': 'frame_id', 'policy': 'policy_id', 'class': 'final_class_id', 'viewpoint': 'viewpoint_candidate'}.get(attack, attack)
        value['source'][f] = 'wrong'
    elif attack == 'changed_quad':
        value['polygon'] = QUAD
    elif attack in ('deleted', 'deferred', 'confirmed'):
        value['state'] = attack
    elif attack in ('gt', 'train', 'robot'):
        value[{'gt': 'ground_truth_eligible', 'train': 'automatic_training_eligible', 'robot': 'robot_command_eligible'}[attack]] = True
    elif attack == 'nested_robot':
        value['pose']['robot_command_eligible'] = True
    elif attack == 'auto':
        value['event_kind'] = next(iter(round2.AUTO_EVENTS))
    elif attack == 'machine':
        value['machine_draft'] = True
    elif attack == 'origin':
        value['origin'] = 'unknown_machine'
    elif attack == 'statement':
        s['statement_zh'] = 'Completed something else'
    elif attack == 'scope':
        s['scoped_request_keys'] = []
    with database() as db:
        if attack.startswith('older_'):
            old = payload(r, revision=1)
            if attack == 'older_modified':
                old['polygon'] = QUAD
            elif attack == 'older_confirmed':
                old['ground_truth_eligible'] = True
            else:
                old['machine_transfer_performed'] = True
            db.execute('INSERT INTO events(target_key,revision,payload) VALUES (?,?,?)', ('box', 1, json.dumps(old)))
        raw = json.dumps(value, indent=2)
        db.execute('INSERT INTO annotations VALUES (?,?,?)', ('box', 2, raw))
        if attack != 'absent_event':
            db.execute('INSERT INTO events(target_key,revision,payload) VALUES (?,?,?)',
                ('box', 2, json.dumps(value) if attack == 'different_raw' else raw))
        if attack in ('duplicate_event', 'newer_event'):
            db.execute('INSERT INTO events(target_key,revision,payload) VALUES (?,?,?)',
                ('box', 2 if attack == 'duplicate_event' else 3, raw))
        if attack == 'duplicate_current':
            db.execute('INSERT INTO annotations VALUES (?,?,?)', ('box', 2, raw))
        with pytest.raises(ValueError):
            round2.exact_original_rectangle_review(db, 'box', r, 'snapshot', 'binding', s)


def test_completed_quads_eight_point_and_original_reviews_have_distinct_rendering_branches(tmp_path):
    rs = {r['target_key']: r for r in (row('quad', 5), row('eight', 3), row('original', 4))}
    with database() as db:
        for key, polygon in (('quad', QUAD), ('eight', POLYGON), ('original', None)):
            insert(db, payload(rs[key], polygon))
        candidates = {k: round2.remaining.validated_manual_source(db, k, rs[k], 'snapshot', 'binding') for k in ('quad', 'eight')}
        reviewed = {'original': round2.exact_original_rectangle_review(db, 'original', rs['original'], 'snapshot', 'binding', statement('original'))}
        before = db.total_changes
        manual = round2.completed_manual_proposals([request(r) for r in rs.values()], candidates, reviewed, rs, [],
            'snapshot', 'formal', 'classification')
        assert db.total_changes == before
    bykey = indexed = {p['target_key']: p for p in manual}
    assert bykey['quad']['manual_four_point_source'] and not bykey['quad']['propagation_seed_eligible']
    assert not bykey['eight']['manual_four_point_source'] and bykey['eight']['propagation_seed_eligible']
    assert bykey['eight']['rendering_id'] == 'remaining_round2_generation:eight'
    assert bykey['original']['schema'] == round2.REVIEW_SCHEMA and not bykey['original']['human_edited']
    assert bykey['original']['rendering_id'] == 'remaining_round2_generation:review_source:original'
    assert 'template' not in bykey['original'] and 'human_polygon_revision' not in bykey['original']['human_source']
    templates = round2.generation_templates(candidates, {'quad', 'eight'}, {}, 'formal', 'classification')
    assert set(templates) == {'eight'}
    inventory = round2.rendering_inventory(dict(templates=[], rendering_by_target={}), templates, manual, manual, set())
    assert len(inventory['manual_source_renderings']) == 2 and len(inventory['templates']) == 1
    class Evidence:
        def image(self, frame):
            return np.zeros((16, 24), np.uint8)
    for d in ('cases', 'images', 'masks', 'labelme'):
        (tmp_path/d).mkdir()
    round2.package_manual_preview(tmp_path, manual, Evidence())
    for key, r in rs.items():
        label = round2.read_json(tmp_path/'labelme'/f'{key}.json')
        assert label['flags']['human_edited'] == (key != 'original')
        assert not label['flags']['machine_draft'] and not label['flags']['ground_truth_eligible']
        assert len(label['shapes']) == 1 and label['imagePath'] == '../images/'+key+'.png'
        assert label['shapes'][0]['points'] == bykey[key]['polygon']
        assert np.array_equal(np.asarray(Image.open(tmp_path/'masks'/f'{key}.png')) > 0,
            raster_mask(bykey[key]['polygon'], *r['size']))
    assert set(indexed) == set(rs)


@pytest.mark.parametrize('attack', ['locked_frame', 'locked_key', 'quarantine', 'gallery', 'class', 'split', 'formal', 'missing', 'stale_revision'])
def test_completed_sources_fail_closed(attack):
    r = row('manual', 4)
    with database() as db:
        insert(db, payload(r, QUAD))
        candidates = {'manual': round2.remaining.validated_manual_source(db, 'manual', r, 'snapshot', 'binding')}
    req = request(r); previous = []; quarantine = []; gallery = []
    if attack == 'locked_frame':
        previous = [proposal('locked', 22, r['frame_id'])]
    elif attack == 'locked_key':
        previous = [proposal('manual', 4)]
    elif attack == 'quarantine':
        quarantine = ['manual']
    elif attack == 'gallery':
        gallery = ['manual']
    elif attack in ('class', 'split', 'formal'):
        if attack == 'class':
            req['class_id'] = 1
        else:
            req['source']['split' if attack == 'split' else 'formal_manifest_sha256'] = 'wrong'
    elif attack == 'stale_revision':
        req['existing_pose_revision'] = 2
    else:
        candidates = {}
    with pytest.raises(ValueError):
        round2.completed_manual_proposals([req], candidates, {}, {'manual': r}, previous,
            'snapshot', 'formal', 'classification', quarantine, gallery)


def test_all_validated_gallery_sources_filter_old_five_classes_not_remaining_and_never_quads():
    gallery = {k: dict(row=row(k, c), polygon=POLYGON, origin=round2.original.GALLERY_ORIGIN,
        saved_review_unchanged=True, new_human_edit_created=False) for k, c in (('old5', 1), ('saved', 3))}
    templates = round2.generation_templates({}, set(), gallery, 'formal', 'classification')
    assert set(templates) == {'saved'} and templates['saved']['saved_review_unchanged']
    assert not templates['saved']['new_human_edit_created']
    gallery['saved']['polygon'] = QUAD
    with pytest.raises(ValueError):
        round2.generation_templates({}, set(), gallery, 'formal', 'classification')


def test_all_eight_full_classes_skipped_and_frames_locked_across_classes():
    filled = (1, 2, 3, 6, 9, 10, 13, 15)
    locked = [proposal(f'full{c}-{i}', c) for c in filled for i in range(100)]
    rs = [row(f'c{c}', c) for c in filled]+[row('active', 8), row('crossclass', 19, locked[0]['source']['frame_id'])]
    rows = {r['target_key']: r for r in rs}
    work, outcomes = round2.target_inventory(rows, locked, dict.fromkeys(rows, 'eligible'), set(), set(), set())
    assert [k for k, _ in work] == ['active']
    reasons = {v['target_key']: v['reason'] for v in outcomes}
    assert reasons['c3'] == reasons['c6'] == reasons['c13'] == 'locked_class_quota_full'
    assert reasons['crossclass'] == 'locked_counted_frame'
    selected = round2.expand.select_locked_diverse(locked, [proposal('full3', 3), proposal('new', 8)])[0]
    assert [p['target_key'] for p in selected] == ['new']


@pytest.mark.parametrize('attack', [None, 'old_current', 'old_event', 'unrelated_event'])
def test_source_delta_preserves_raw_history_and_limits_new_writes_to_task_keys(tmp_path, attack):
    old, new = tmp_path/'old.sqlite3', tmp_path/'new.sqlite3'
    with database(old) as db:
        insert(db, payload(row('old'), QUAD))
    import shutil
    shutil.copyfile(old, new)
    with sqlite3.connect(new) as db:
        insert(db, payload(row('task'), QUAD))
        if attack == 'old_current':
            db.execute("UPDATE annotations SET payload='{}' WHERE target_key='old'")
        elif attack == 'old_event':
            db.execute("UPDATE events SET payload='{}' WHERE target_key='old'")
        elif attack == 'unrelated_event':
            db.execute("INSERT INTO events(target_key,revision,payload) VALUES ('unrelated',1,'{}')")
    if attack:
        with pytest.raises(ValueError):
            round2.validate_source_delta(old, new, 'annotations', {'task'})
    else:
        assert round2.validate_source_delta(old, new, 'annotations', {'task'}) == dict(
            current_changed_keys=['task'], new_events=1, historical_events_preserved=1, unrelated_source_changes=0)


def test_chinese_report_all22_and_strict_scope(tmp_path):
    fields = ('old_total', 'manual_modified_added', 'reviewed_original_rectangle', 'saved_human',
        'new_candidates', 'total', 'missing', 'saved_pose_human', 'saved_gallery_human',
        'db_stored_human_review', 'capacity_matching_distinct_frames')
    counts = {c: dict.fromkeys(fields, 0) for c in round2.ALL_CLASSES}
    round2.build_report(tmp_path, [], counts, set(), set(), set(), {}, set(), dict(unchanged_keys=[]), {})
    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 round2.ALL_CLASSES)
    assert round2.USER_STATEMENT in text and '132份轮廓变化、53份复核后保持原候选' in text
    assert '47份四点、4份类别3八点' in text and '旧两份原框复核与旧45份侧记' in text
    assert '1350个已计入帧全部锁定' in text and 'max-width:1090px' in text and 'overflow-x:auto' in text
    assert round2.read_json(tmp_path/'font_coverage.json')['all_report_Chinese_present']


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


def test_rendering_inventory_does_not_mutate_historical_objects():
    old = proposal('old'); old['rendering_id'] = 'old_generation:seed'
    parent = dict(templates=[dict(rendering_id=old['rendering_id'], polygon=POLYGON)],
        manual_source_renderings=[], rendering_by_target={'old': old['rendering_id']})
    before = canonical(parent)
    inv = round2.rendering_inventory(parent, {}, [], [old], set())
    assert canonical(parent) == before and inv['templates'] == parent['templates']
    assert inv['publication_version'] == 12
