"""Publication-8 contracts; all mutation fixtures use temporary/in-memory stores."""
import copy
import hashlib
import json
import sqlite3

import numpy as np
import pytest
from PIL import Image

from pallet import mask_quota_100 as parent
from pallet import mask_quota_100_continue as quota
from pallet import mask_quota_completion as original
from pallet.mask_template_transfer import annotation_inventory
from pallet.pose_review_geometry import raster_mask


POLYGON = [[1, 1], [20, 1], [20, 10], [15, 10], [15, 5], [5, 5], [5, 10], [1, 10]]


def row(key, cls=10):
    return dict(target_key=key, target_id='target-'+key, frame_id='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 proposal(key, cls=10):
    return dict(target_key=key, class_id=cls, source=row(key, cls),
        polygon=copy.deepcopy(POLYGON), vertex_count=8,
        metrics=dict(boundary_ncc=.9, ecc=.95), template=dict(target_key='shared'))


def gallery_review(key):
    return dict(target_key=key, polygon=copy.deepcopy(POLYGON), revision=7,
        human_edited=True, event_kind='human_mask_gallery_revision')


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


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, key='manual', cls=9, polygon=QUAD, auto=True):
    r = row(key, cls)
    human = dict(target_key=key, revision=6, state='draft', source=r,
        polygon=copy.deepcopy(polygon), **dict.fromkeys(quota.FALSE_FLAGS, False))
    event_raw = json.dumps(human, indent=2)
    value = dict(human, revision=7, event_kind='unit_normalization') if auto else human
    raw = json.dumps(value, indent=1)
    db.execute('INSERT INTO annotations VALUES (?,?,?)', (key, value['revision'], raw))
    db.execute('INSERT INTO events(target_key,revision,payload) VALUES (?,?,?)', (key, 6, event_raw))
    if auto:
        db.execute('INSERT INTO events(target_key,revision,payload) VALUES (?,?,?)', (key, 7, raw))
    request = dict(target_key=key, class_id=cls, source=dict(r, formal_manifest_sha256='formal'),
        pose_annotation_url='https://pose.example/?target='+key)
    return r, request, raw, event_raw


def make_manual(db, rows, requests, previous=(), quarantine=()):
    current, seeds, status = annotation_inventory(db, rows)
    manual, records = quota.manual_source_proposals(db, requests, seeds, current, rows, previous,
        'snapshot', 'formal', 'classes', quarantine)
    return manual, records, current, seeds, status


def test_frozen_engines_constants_and_provenance_are_not_mutated():
    assert quota.PUBLICATION_VERSION == 8 and quota.TARGET_PER_CLASS == 100
    assert quota.GENERATION_NAMESPACE == 'quota100_continue_generation'
    assert quota.OUTPUT.name == 'pallet_mask_quota_100_continue_v1'
    assert quota.PARENT.name == 'pallet_mask_quota_100_v2'
    assert quota.REQUESTS.name == 'pallet_mask_quota_100_requests_v2'
    assert set(quota.SOURCE_PUBLICATIONS) == {3, 4, 5, 6, 7}
    assert quota.SOURCE_PUBLICATIONS[7] == quota.PARENT
    assert parent.PUBLICATION_VERSION == 7 and parent.GENERATION_NAMESPACE == 'quota100_generation'
    assert set(parent.SOURCE_PUBLICATIONS) == {3, 4, 5, 6}
    assert original.PUBLICATION_VERSION == 6
    assert quota.attempt_target is parent.attempt_target
    assert quota.select_diverse is parent.select_diverse


def test_four_point_source_uses_actual_human_event_not_latest_auto_or_fake_vertices():
    with database() as db:
        r, request, raw, event_raw = save(db)
        changes = db.total_changes
        manual, records, current, seeds, status = make_manual(db, {'manual': r}, [request])
        assert db.total_changes == changes
    p, record = manual[0], records[0]
    assert p['polygon'] == QUAD and p['vertex_count'] == 4
    assert p['manual_four_point_source'] is True and p['propagation_seed_eligible'] is False
    assert 'template' not in p and p['human_source']['vertex_count'] == 4
    assert p['human_source']['human_polygon_revision'] == 6 and p['target_expected_revision'] == 7
    assert p['human_source']['human_event_sha256'] == hashlib.sha256(event_raw.encode()).hexdigest()
    assert p['human_source']['human_event_sha256'] != quota.sha(json.loads(event_raw))
    assert p['rendering_id'] == 'quota100_continue_generation:manual_source:manual'
    assert p['metrics'] == {} and 'continuation' not in p and 'generation_method' not in p
    assert not any(p[f] for f in quota.FALSE_FLAGS)
    assert not p['new_human_edit_created'] and not p['machine_transfer_performed']
    assert not p['pose_store_written'] and not p['gallery_store_written']
    assert not seeds and status['manual'] == 'human_mask_history_protected'
    assert record['current_payload_raw'] == raw and record['human_event_payload_raw'] == event_raw
    assert record['current_payload_raw_sha256'] == hashlib.sha256(raw.encode()).hexdigest()
    assert record['human_event_sha256'] == hashlib.sha256(event_raw.encode()).hexdigest()
    assert record['vertex_count'] == 4 and record['manual_four_point_source'] is True
    assert record['propagation_seed_eligible'] is False
    counts = quota.quota_counts([], [], {}, current, seeds, manual=manual)
    assert counts[9]['saved_human'] == counts[9]['saved_pose_human'] == 1
    assert counts[9]['manual_four_point_sources'] == 1 and counts[9]['saved_fine_edits'] == 0
    assert counts[9]['total'] == 1 and counts[9]['missing'] == 99


def test_24_actual_manual_sources_count_but_only_23_become_seeds():
    with database() as db:
        rows, requests = {}, []
        for n, cls in enumerate((1,)*10+(10,)*10+(9,)*2+(15,)*2):
            key = 'manual-'+str(n)
            r, request, _, _ = save(db, key, cls, QUAD if n == 20 else POLYGON)
            rows[key] = r; requests.append(request)
        manual, records, current, seeds, _ = make_manual(db, rows, requests)
    previous = [proposal(f'old-{cls}-{n}', cls) for cls, count in ((1, 58), (2, 100), (9, 83), (10, 54), (15, 83))
        for n in range(count)]
    c = quota.quota_counts(previous, [], {}, current, seeds, manual=manual)
    assert len(manual) == len(records) == 24 and len(seeds) == 23
    assert sum(x['saved_human'] for x in c.values()) == 24
    assert {cls: x['total'] for cls, x in c.items()} == {1: 68, 2: 100, 9: 85, 10: 64, 15: 85}
    assert sum(x['total'] for x in c.values()) == 402
    assert all(p['schema'] == quota.MANUAL_SCHEMA for p in manual)
    assert all('template' in p for p in manual if not p['manual_four_point_source'])


@pytest.mark.parametrize('attack', ['rectangle', 'no_event', 'auto_only', 'different_latest_human',
    'deleted', 'deferred', 'confirmed', 'training', 'robot', 'event_source', 'current_source',
    'quarantine', 'prior_key', 'prior_frame', 'duplicate_request', 'request_source', 'request_class',
    'request_formal', 'event_revision', 'self_intersection', 'triangle'])
def test_four_point_sources_require_real_current_modified_human_provenance(attack):
    with database() as db:
        r, request, raw, event_raw = save(db, auto=False)
        value, event = json.loads(raw), json.loads(event_raw)
        previous, quarantine, requests = [], set(), [request]
        if attack == 'rectangle':
            value['polygon'] = event['polygon'] = [[1, 1], [20, 1], [20, 10], [1, 10]]
        elif attack == 'no_event':
            db.execute('DELETE FROM events')
        elif attack == 'auto_only':
            event['event_kind'] = 'axes_normalization'
        elif attack == 'different_latest_human':
            event['polygon'][0][0] += .5
        elif attack in ('deleted', 'deferred'):
            value['state'] = attack
        elif attack in ('confirmed', 'training', 'robot'):
            value[dict(confirmed='human_mask_confirmed', training='automatic_training_eligible',
                robot='robot_command_eligible')[attack]] = True
        elif attack in ('event_source', 'current_source'):
            (event if attack == 'event_source' else value)['source']['split'] = 'test'
        elif attack == 'quarantine':
            quarantine.add('manual')
        elif attack == 'prior_key':
            previous = [proposal('manual')]
        elif attack == 'prior_frame':
            previous = [dict(proposal('old'), source=r)]
        elif attack == 'duplicate_request':
            requests.append(copy.deepcopy(request))
        elif attack.startswith('request_'):
            if attack == 'request_class':
                request['class_id'] = 2
            else:
                request['source']['formal_manifest_sha256' if attack == 'request_formal' else 'frame_id'] = 'wrong'
        elif attack == 'event_revision':
            event['revision'] = 2
        elif attack == 'self_intersection':
            value['polygon'] = event['polygon'] = [QUAD[0], QUAD[2], QUAD[1], QUAD[3]]
        else:
            value['polygon'] = event['polygon'] = QUAD[:3]
        db.execute('UPDATE annotations SET payload=?', (json.dumps(value),))
        db.execute('UPDATE events SET payload=?', (json.dumps(event),))
        with pytest.raises(ValueError):
            make_manual(db, {'manual': r}, requests, previous, quarantine)


def test_four_point_not_in_propagation_or_rendering_templates_but_can_render_source(tmp_path):
    with database() as db:
        r, request, _, _ = save(db)
        manual, _, current, seeds, _ = make_manual(db, {'manual': r}, [request])
    old = proposal('old'); machine = proposal('machine')
    old_inventory = dict(templates=[dict(rendering_id='historical:shared')],
        rendering_by_target={'old': 'historical:shared'})
    templates = {'shared': dict(row=row('shared'), origin=quota.POSE_ORIGIN)}
    inventory = quota.rendering_inventory(old_inventory, templates, [old, machine]+manual, {'machine'}, manual)
    assert inventory['generation_template_keys'] == ['shared']
    assert 'manual' not in {t.get('row', {}).get('target_key') for t in inventory['templates']}
    assert inventory['manual_source_renderings'][0]['polygon'] == QUAD
    assert inventory['rendering_by_target']['manual'] == manual[0]['rendering_id']
    assert inventory['deferred_manual_template_keys'] == ['manual']
    class Evidence:
        def image(self, frame):
            return np.zeros((16, 24), np.uint8)
    for name in ('cases', 'images', 'labelme', 'masks'):
        (tmp_path/name).mkdir()
    quota.package_manual_preview(tmp_path, manual, Evidence())
    label = json.loads((tmp_path/'labelme/manual.json').read_text())
    assert label['shapes'][0]['points'] == QUAD and len(label['shapes'][0]['points']) == 4
    assert 'template' not in label['mask_template_provenance']
    assert not label['flags']['machine_draft']
    assert np.array_equal(np.asarray(Image.open(tmp_path/'masks/manual.png')) > 0, raster_mask(QUAD, 24, 16))


def test_saved_review_evidence_39_of_84_not_claimed_completion_and_no_pending_seeds():
    old_new = [proposal('prior-'+str(n)) for n in range(84)]
    saved = {p['target_key']: {} for p in old_new[:39]}
    status = quota.saved_review_status(old_new, saved, saved)
    assert status['actually_saved'] == 39 and status['pending'] == 45
    assert len(status['pending_keys']) == 45 and not status['user_completion_claim_used_as_evidence']
    with pytest.raises(ValueError, match='Unsaved'):
        quota.saved_review_status(old_new, saved, {'prior-80': {}})


def test_203_gallery_plus_111_manual_is_314_saved_with_88_old_pending():
    previous = [proposal(f'old-{cls}-{n}', cls) for cls, count in ((1, 58), (2, 100), (9, 83), (10, 54), (15, 83))
        for n in range(count)]
    inherited = previous[:87]
    current = {p['target_key']: dict(state='draft', polygon=POLYGON) for p in inherited}
    seeds = {key: dict(polygon=POLYGON) for key in current}
    reviews = {p['target_key']: gallery_review(p['target_key']) for p in previous[87:290]}
    with database() as db:
        rows, requests = {}, []
        for n in range(24):
            key = 'manual-'+str(n); cls = (1,)*10+(10,)*10+(9,)*2+(15,)*2
            r, request, _, _ = save(db, key, cls[n], QUAD if n == 20 else POLYGON)
            rows[key] = r; requests.append(request)
        manual, _, actual, direct, _ = make_manual(db, rows, requests)
    current.update(actual); seeds.update(direct)
    counts = quota.quota_counts(previous, [], reviews, current, seeds, inherited, manual)
    assert sum(c['saved_human'] for c in counts.values()) == 314
    assert sum(c['saved_pose_human'] for c in counts.values()) == 111
    assert sum(c['saved_gallery_human'] for c in counts.values()) == 203
    assert sum(c['old_pending_machine'] for c in counts.values()) == 88


def test_full_class_2_cannot_gain_reused_humans_and_reuse_never_fabricates_requests():
    with database() as db:
        rows = {}
        for key, cls in (('full', 2), ('under', 1)):
            r, _, _, _ = save(db, key, cls, POLYGON); rows[key] = r
        current, templates, _ = annotation_inventory(db, rows)
    previous = [proposal('old-'+str(n), 2) for n in range(100)]
    requested = [dict(proposal('request'), original_pose_url='https://pose.example/?target=request')]
    reused, inventory = quota.existing_human_reuse(templates, current, rows, previous, requested,
        'snapshot', 'formal', 'classes')
    assert [p['target_key'] for p in reused] == ['under']
    assert {r['target_key']: r['reason'] for r in inventory} == {'full': 'quota_full', 'under': 'counted'}
    assert 'manual_request_sha256' not in reused[0]
    assert reused[0]['rendering_id'] == quota.GENERATION_NAMESPACE+':under'


def test_old_assets_and_all_five_historical_namespaces_preserved(tmp_path):
    parent_dir, out = tmp_path/'parent', tmp_path/'out'
    for root in (parent_dir, out):
        for folder in ('cases', 'images', 'labelme', 'masks', 'templates'):
            (root/folder).mkdir(parents=True)
    for folder, ext in (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json')):
        (parent_dir/folder/f'old.{ext}').write_bytes(b'canonical-old')
    namespaces = ('templates', 'refined/templates', 'generation/templates', 'quota_generation/templates',
        'quota100_generation/templates')
    for ns in namespaces:
        (parent_dir/ns).mkdir(parents=True, exist_ok=True)
        (parent_dir/ns/'shared.json').write_bytes(ns.encode())
    (out/'templates/shared.json').write_bytes(b'new-direct-human')
    hashes = quota.preserve_parent_assets(parent_dir, out, [dict(target_key='old')])
    assert all(quota.digest(out/name) == value for name, value in hashes.items())
    for ns in namespaces:
        assert (out/ns/'shared.json').read_bytes() == ns.encode()
    assert (out/quota.GENERATION_NAMESPACE/'templates/shared.json').read_bytes() == b'new-direct-human'


def test_machine_contract_and_explicit_100_cap_are_unchanged():
    with database() as db:
        r, request, _, _ = save(db, polygon=POLYGON)
        _, _, current, pose, _ = make_manual(db, {'manual': r}, [request])
    templates = original.seed_union(pose, {}, 'snapshot')
    accepted = dict(template_key='manual', polygon=POLYGON, template_rank=8,
        metrics=dict(boundary_guard=dict(state='passed')))
    before = copy.deepcopy((templates, accepted))
    p = quota.proposal_from_attempt('new', row('new', 9), accepted, templates, {}, 'snapshot', 'formal', 'classes')
    assert (templates, accepted) == before and p['continuation']['batch'] == 8
    assert p['schema'] == quota.VERSION and p['target_per_class'] == 100
    assert p['generation_method']['name'] == 'original' and not any(p[f] for f in quota.FALSE_FLAGS)
    full = [proposal('old-'+str(n), 2) for n in range(100)]
    selected, reasons = quota.select_diverse(full, [proposal('extra', 2)], goal=quota.TARGET_PER_CLASS)
    assert selected == [] and reasons['extra'] == 'accepted_quota_full_not_selected'
    with pytest.raises(ValueError, match='exceeds goal'):
        quota.quota_counts(full, [proposal('extra', 2)], {}, {}, {})


def test_report_is_chinese_responsive_default_new_machine_only_and_honest(tmp_path):
    with database() as db:
        r, request, _, _ = save(db)
        manual, _, current, seeds, _ = make_manual(db, {'manual': r}, [request])
    previous, new = [proposal('old', 1)], [proposal('machine', 9)]
    counts = quota.quota_counts(previous, new, {}, current, seeds, manual=manual)
    quota.build_report(tmp_path, previous+manual+new, {'machine'}, counts, {}, current, manual=manual,
        review_status=dict(parent_new_machine_candidates=84, actually_saved=39, pending=45))
    page = (tmp_path/'report_zh.html').read_text()
    assert '<html lang="zh-CN">' in page and '@media(max-width:700px)' in page
    assert 'overflow-x:auto' in page and 'min-width:1100px' in page
    assert 'id="only-new" checked>只看本轮新增机器候选' in page
    assert page.count('data-batch="new"') == 1 and 'data-kind="manual" hidden' in page
    assert '实际已保存复核 39/84 份，另 45 份未保存，不作种子' in page
    assert '计入覆盖，不作传播模板' in page and '不补造顶点' in page
    assert '本轮新增机器候选 1 份' in page and '四点来源 1 份' in page
    assert '未确认真值，不自动进入训练或机器人控制' in page
    from fontTools.ttLib import TTFont
    with TTFont(tmp_path/'report-font.otf') as font:
        assert {ord(c) for c in page if '\u3400' <= c <= '\u9fff'} <= set(font.getBestCmap())


def test_sources_must_still_be_current_at_completion(tmp_path):
    live, snapshot = tmp_path/'live.sqlite3', tmp_path/'snapshot.sqlite3'
    with sqlite3.connect(live) as db:
        db.executescript('CREATE TABLE reviews(target_key TEXT,revision INTEGER,payload TEXT);'
            'CREATE TABLE events(id INTEGER PRIMARY KEY,target_key TEXT,revision INTEGER,payload TEXT);')
        db.execute('INSERT INTO reviews VALUES (?,?,?)', ('key', 1, '{}'))
        db.execute('INSERT INTO events VALUES (?,?,?,?)', (1, 'key', 1, '{}'))
    quota.snapshot_database(live, snapshot)
    quota.verify_sources_current(live, snapshot, 'reviews')
    with sqlite3.connect(live) as db:
        db.execute('UPDATE reviews SET revision=2')
    with pytest.raises(ValueError, match='Concurrent human saves'):
        quota.verify_sources_current(live, snapshot, 'reviews')


def test_old_or_non_original_output_is_never_overwritten(tmp_path):
    existing = tmp_path/'existing'; existing.mkdir(); marker = existing/'user.txt'; marker.write_text('keep')
    with pytest.raises(ValueError, match='new outputs directory'):
        quota.generate(existing)
    assert marker.read_text() == 'keep'
    for kwargs, message in ((dict(method='multiscale'), 'Only the frozen original'), (dict(workers=0), 'Workers')):
        out = tmp_path/'new'
        with pytest.raises(ValueError, match=message):
            quota.generate(out, **kwargs)
        assert not out.exists()
