"""Publication-9 composition contracts; mutation tests use only temporary stores."""
import copy
import hashlib
import json
import sqlite3
import zipfile

import numpy as np
import pytest
from PIL import Image

from pallet import mask_quota_100_complete as quota
from pallet import mask_quota_100_continue as prior
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]]
QUAD = [[1.1, 1.5], [20, 1], [20.1, 9], [1, 10]]


def row(key, cls=9):
    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=9):
    return dict(target_key=key, class_id=cls, source=row(key, cls),
        polygon=copy.deepcopy(POLYGON), vertex_count=8, template=dict(target_key='shared'))


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):
    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')
    raw = json.dumps(value, indent=1)
    db.execute('INSERT INTO annotations VALUES (?,?,?)', (key, 7, raw))
    db.execute('INSERT INTO events(target_key,revision,payload) VALUES (?,?,?)', (key, 6, event_raw))
    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 manual_proposals(db, rows, requests, previous=(), quarantine=()):
    current, seeds, status = quota.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_constants_freeze_parent_engines_and_new_namespace():
    assert quota.PUBLICATION_VERSION == 9 and quota.TARGET_PER_CLASS == 100
    assert quota.GENERATION_NAMESPACE == 'quota100_complete_generation'
    assert quota.OUTPUT.name == 'pallet_mask_quota_100_complete_v1'
    assert quota.PARENT == prior.OUTPUT
    assert quota.REQUESTS.name == 'pallet_mask_quota_100_final_requests_v2'
    assert set(quota.SOURCE_PUBLICATIONS) == {3, 4, 5, 6, 7, 8}
    assert prior.PUBLICATION_VERSION == 8 and prior.GENERATION_NAMESPACE == 'quota100_continue_generation'
    assert not hasattr(quota, 'attempt_target') and not hasattr(quota, 'select_diverse')


def test_four_point_manual_is_exact_source_not_generated_template(tmp_path):
    with database() as db:
        r, request, raw, event_raw = save(db)
        changes = db.total_changes
        manual, records, _, seeds, _ = manual_proposals(db, {'manual': r}, [request])
        assert db.total_changes == changes
    p, record = manual[0], records[0]
    assert p['generation_schema'] == quota.VERSION and p['schema'] == quota.MANUAL_SCHEMA
    assert p['rendering_id'] == 'quota100_complete_generation:manual_source:manual'
    assert p['polygon'] == QUAD and p['vertex_count'] == 4 and 'template' not in p
    assert p['human_source']['human_event_sha256'] == hashlib.sha256(event_raw.encode()).hexdigest()
    assert p['human_source']['human_polygon_revision'] == 6 and p['target_expected_revision'] == 7
    assert record['current_payload_raw'] == raw and record['human_event_payload_raw'] == event_raw
    assert record['manual_four_point_source'] and not record['propagation_seed_eligible']
    assert not seeds and p['metrics'] == {} and not p['machine_transfer_performed']
    assert not p['new_human_edit_created'] and not any(p[f] for f in quota.FALSE_FLAGS)
    for directory in ('cases', 'images', 'masks', 'labelme'):
        (tmp_path/directory).mkdir()
    class Evidence:
        def image(self, frame):
            return np.zeros((16, 24), np.uint8)
    quota.prior.package_manual_preview(tmp_path, manual, Evidence())
    label = quota.read_json(tmp_path/'labelme/manual.json')
    assert label['shapes'][0]['points'] == QUAD and 'template' not in label['mask_template_provenance']
    assert not label['flags']['machine_draft'] and not label['flags']['ground_truth_eligible']
    assert np.array_equal(np.asarray(Image.open(tmp_path/'masks/manual.png')) > 0, raster_mask(QUAD, 24, 16))


@pytest.mark.parametrize('attack', ['rectangle', 'no_event', 'auto_only', 'wrong_human', 'confirmed',
    'training', 'robot', 'quarantined', 'prior_key', 'duplicate_request', 'request_class', 'source_changed'])
def test_manual_source_rejects_invented_or_ineligible_provenance(attack):
    with database() as db:
        r, request, raw, event_raw = save(db)
        value, event = json.loads(raw), json.loads(event_raw)
        requests, previous, quarantine = [request], [], set()
        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 == 'wrong_human':
            event['polygon'][0][0] += 1
        elif attack in ('confirmed', 'training', 'robot'):
            value[dict(confirmed='human_mask_confirmed', training='automatic_training_eligible',
                robot='robot_command_eligible')[attack]] = True
        elif attack == 'quarantined':
            quarantine.add('manual')
        elif attack == 'prior_key':
            previous = [proposal('manual')]
        elif attack == 'duplicate_request':
            requests += [copy.deepcopy(request)]
        elif attack == 'request_class':
            request['class_id'] = 2
        else:
            value['source']['split'] = 'test'
        db.execute('UPDATE annotations SET payload=?', (json.dumps(value),))
        db.execute('UPDATE events SET payload=? WHERE revision=6', (json.dumps(event),))
        with pytest.raises(ValueError):
            manual_proposals(db, {'manual': r}, requests, previous, quarantine)


def test_inherited_four_and_latest_source_audit_do_not_overwrite_canonical():
    with database() as db:
        r, request, raw, event_raw = save(db)
        manual, _, _, _, _ = manual_proposals(db, {'manual': r}, [request])
        old = copy.deepcopy(manual)
        manual[0]['target_snapshot_payload_sha256'] = 'historical'
        manual[0]['target_expected_revision'] = 3
        manual[0]['polygon'] = copy.deepcopy(POLYGON)
        before = copy.deepcopy(manual); changes = db.total_changes
        sources, audit = quota.audit_inherited_manual(db, manual, 'latest-snapshot')
        assert db.total_changes == changes and manual == before
    assert audit[0]['source_changed'] and audit[0]['polygon_changed']
    assert not audit[0]['canonical_proposal_overwritten'] and audit[0]['latest_active_direct_human']
    assert sources[0]['current_payload_raw'] == raw and sources[0]['human_event_payload_raw'] == event_raw
    assert sources[0]['vertex_count'] == 4 and not sources[0]['propagation_seed_eligible']
    assert sources[0]['current_payload']['polygon'] == old[0]['polygon']


def old45_fixture():
    proposals = [proposal('old-'+str(n), (1, 2, 9, 10, 15)[n % 5]) for n in range(84)]
    source = [dict(target_key=p['target_key'], class_id=p['class_id'], frame_id=p['source']['frame_id'],
        status='no_server_review_saved') for p in proposals[:45]]
    return proposals, source


def test_exact_old45_no_change_statement_never_becomes_human_save_or_seed():
    old, source = old45_fixture(); before = copy.deepcopy((old, source))
    statement = '这45份已经看过，不需要修改。'
    values = quota.review_dispositions(source, old, old, {}, statement)
    assert (old, source) == before and len(values) == 45
    for value in values:
        p = quota.indexed(old)[value['target_key']]
        assert value['source_proposal_id'] == quota.sha(p) and value['polygon_sha256'] == quota.sha(p['polygon'])
        assert value['disposition'] == 'user_reviewed_no_change'
        assert value['origin'] == 'explicit_user_statement_in_conversation'
        assert value['user_statement_sha256'] == hashlib.sha256(statement.encode()).hexdigest()
        for field in ('human_edited', 'creates_gallery_revision', 'human_mask_confirmed', 'seed_authorization',
                'automatic_training_eligible', 'robot_command_eligible', 'ground_truth_eligible'):
            assert value[field] is False


@pytest.mark.parametrize('attack', ['scope44', 'scope46', 'foreign_key', 'class', 'frame', 'saved',
    'previous_changed', 'empty_statement'])
def test_no_change_statement_cannot_expand_to_other43_or_current1(attack):
    old, source = old45_fixture(); previous = copy.deepcopy(old); saved = {}; statement = '明确无需修改'
    if attack == 'scope44':
        source.pop()
    elif attack == 'scope46':
        source.append(dict(source[-1], target_key=old[46]['target_key']))
    elif attack == 'foreign_key':
        source[0]['target_key'] = 'current-missing'
    elif attack in ('class', 'frame'):
        source[0]['class_id' if attack == 'class' else 'frame_id'] = 'changed'
    elif attack == 'saved':
        saved[source[0]['target_key']] = {}
    elif attack == 'previous_changed':
        previous[0]['polygon'][0][0] += 1
    else:
        statement = ''
    with pytest.raises(ValueError):
        quota.review_dispositions(source, old, previous, saved, statement)


def test_475_plus25_is500_but_saved411_no_change45_and_no_new_review44():
    previous = [proposal(f'old-{cls}-{n}', cls) for cls, count in
        ((1, 100), (2, 100), (9, 90), (10, 85), (15, 100)) for n in range(count)]
    manual = [proposal('manual-'+str(n), 9 if n < 10 else 10) for n in range(25)]
    combined = previous+manual
    sources = [dict(target_key=p['target_key'], manual_four_point_source=i in (0, 118))
        for i, p in enumerate(previous[:118]+manual)]
    reviews = {p['target_key']: dict(human_edited=True, event_kind='human_mask_gallery_revision', polygon=POLYGON)
        for p in previous[118:386]}
    dispositions = [dict(target_key=p['target_key']) for p in previous[386:431]]
    counts = quota.quota_counts(previous, manual, reviews, sources, dispositions)
    total = lambda field: sum(v[field] for v in counts.values())
    assert len(combined) == total('total') == 500 and total('missing') == 0
    assert all(v['total'] == v['distinct_frames'] == 100 for v in counts.values())
    assert total('saved_human') == 411 and total('saved_gallery_human') == 268
    assert total('saved_pose_human') == 143 and total('manual_four_point_sources') == 2
    assert total('pending_machine_raw') == 89 and total('user_reviewed_no_change') == 45
    assert total('no_new_server_review') == 44 and total('new_candidates') == 0
    assert total('saved_fine_edits') == 409
    with pytest.raises(ValueError, match='exceeds goal'):
        quota.quota_counts(previous, manual+[proposal('extra', 1)], reviews, sources, dispositions)
    with pytest.raises(ValueError, match='repeats a frame'):
        quota.quota_counts(previous, [dict(manual[0], source=previous[0]['source'])]+manual[1:], reviews, sources, dispositions)


def test_current66_has65_saves_one_missing_not_authorized_by_old45():
    current = [proposal('current-'+str(n)) for n in range(66)]
    old, source = old45_fixture()
    dispositions = quota.review_dispositions(source, old, old, {}, '无需修改')
    gallery = {p['target_key']: {} for p in current[:65]}
    older_pending = [proposal('older-'+str(n)) for n in range(43)]
    status = quota.saved_review_status(current, gallery, dispositions, gallery, old[:45]+older_pending+current)
    assert status['actually_saved'] == 65 and status['pending'] == 1
    assert status['pending_keys'] == ['current-65']
    assert status['pending_machine_raw'] == 89 and status['no_new_server_review'] == 44
    assert len(status['older_pending_excluding_acknowledged']) == 43
    assert not status['user_completion_claim_used_as_saved_mask_evidence']
    assert status['no_inference_user_did_not_view']


def test_all_six_namespaces_and_old_manual_rendering_preserved(tmp_path):
    parent, out = tmp_path/'parent', tmp_path/'out'
    for root in (parent, out):
        for directory in ('cases', 'images', 'masks', 'labelme'):
            (root/directory).mkdir(parents=True)
    for directory, extension in (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json')):
        (parent/directory/f'old.{extension}').write_bytes(b'original')
    for directory in quota.HISTORICAL_NAMESPACES:
        (parent/directory).mkdir(parents=True)
        (parent/directory/'same.json').write_bytes(directory.encode())
    hashes = quota.preserve_parent_assets(parent, out, [proposal('old')])
    assert all(quota.digest(out/name) == expected for name, expected in hashes.items())
    for directory in quota.HISTORICAL_NAMESPACES:
        assert (out/directory/'same.json').read_bytes() == directory.encode()
    old_source = dict(target_key='old', rendering_id='old:manual_source:old', polygon=QUAD,
        manual_four_point_source=True, propagation_seed_eligible=False)
    inventory = dict(templates=[], manual_source_renderings=[old_source],
        rendering_by_target={'old': old_source['rendering_id']})
    with database() as db:
        r, request, _, _ = save(db)
        manual, _, _, seeds, _ = manual_proposals(db, {'manual': r}, [request])
    result = quota.rendering_inventory(inventory, seeds, [proposal('old')]+manual, manual)
    assert result['manual_source_renderings'][0] == old_source
    assert len(result['manual_source_renderings']) == 2 and result['templates'] == []
    assert result['deferred_manual_template_keys'] == ['manual', 'old']
    assert result['rendering_by_target']['old'] == 'old:manual_source:old'


def test_report_defaults_to_all_cards_and_new_filter_is_manual_only(tmp_path):
    with database() as db:
        r, request, _, _ = save(db)
        manual, sources, current, _, _ = manual_proposals(db, {'manual': r}, [request])
    previous = [proposal('missing'), proposal('acknowledged'), proposal('older')]
    dispositions = [dict(target_key='acknowledged')]
    counts = quota.quota_counts(previous, manual, {}, sources, dispositions)
    status = dict(pending_keys=['missing'], actually_saved=65, parent_new_machine_candidates=66, pending=1)
    quota.build_report(tmp_path, previous+manual, counts, {}, current, [], manual, dispositions, status)
    page = (tmp_path/'report_zh.html').read_text()
    assert '<html lang="zh-CN">' in page and 'id="only-new">只看本轮计入人工标签' in page
    assert 'id="only-new" checked' not in page and 'data-kind="manual" hidden' not in page
    assert page.count('data-batch="new"') == 1 and page.count('<article id="target-') == 4
    assert '旧45份无需重复复核' in page and '不据此断言用户没有查看' in page
    assert '本轮新增机器候选为0' in page and '不补造顶点' in page
    assert '不自动进入训练或机器人控制' in page and '真实四点' 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_public_archive_is_allowlisted_and_excludes_private_sources(tmp_path):
    names = ('report_zh.html', 'report-font.otf', 'FONT_LICENSE.txt', 'preview_all_classes.png',
        'proposals.jsonl', 'new_proposals.jsonl', 'manual_proposals.jsonl', 'review_dispositions.jsonl')
    for name in names:
        (tmp_path/name).write_bytes(b'public')
    for directory, extension in (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json')):
        (tmp_path/directory).mkdir(); (tmp_path/directory/f'key.{extension}').write_bytes(b'public')
    for name in ('access_key.txt', 'reviews_snapshot.sqlite3', 'all_manual_sources.jsonl'):
        (tmp_path/name).write_bytes(b'private')
    members = quota.package_public_zip(tmp_path, [proposal('key')])
    with zipfile.ZipFile(tmp_path/'pallet_mask_refined_review.zip') as archive:
        assert set(archive.namelist()) == set(members)
        assert all(archive.read(name) == b'public' for name in archive.namelist())
    assert not quota.read_json(tmp_path/'public_archive_inventory.json')['private_files_included']


def test_existing_output_and_missing_user_statement_are_rejected(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.compose(existing, user_statement='explicit')
    assert marker.read_text() == 'keep'
    with pytest.raises(ValueError, match='Explicit user statement'):
        quota.compose(quota.OUTPUT.parent/'not-created-test', user_statement='')
