"""Saved-human extension contracts; production generation is never invoked."""
import copy
import hashlib
import json
from pathlib import Path
import sqlite3

import numpy as np
import pytest

from pallet import mask_quota_completion as original
from pallet import mask_quota_saved_extension as extension


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 make_supplements(db, rows):
    records = []
    for n, key in enumerate(sorted(extension.SUPPLEMENT_KEYS)):
        rows[key] = r = row(key)
        human = dict(target_key=key, revision=3, state='draft', source=r,
            polygon=copy.deepcopy(POLYGON), **dict.fromkeys(original.FALSE_FLAGS, False))
        current = dict(human, revision=9, event_kind='unit_normalization')
        raw = json.dumps(current, indent=1)
        human_raw = json.dumps(human, indent=2)  # exercise exact, non-canonical human event bytes
        db.execute('INSERT INTO annotations VALUES (?,?,?)', (key, 9, raw))
        history = []
        for event_id, revision, payload in ((n*2+1, 3, human_raw), (n*2+2, 9, raw)):
            db.execute('INSERT INTO events VALUES (?,?,?,?)', (event_id, key, revision, payload))
            history.append(dict(id=event_id, revision=revision, payload_raw=payload,
                payload_raw_sha256=hashlib.sha256(payload.encode()).hexdigest()))
        records.append(dict(schema='existing_saved_pose_mask_reuse_diagnostic_v1', target_key=key,
            class_id=10, source=r, polygon=copy.deepcopy(POLYGON), vertex_count=8,
            current_payload=current, current_payload_raw=raw,
            current_payload_sha256=extension.sha(current),
            current_payload_raw_sha256=hashlib.sha256(raw.encode()).hexdigest(),
            human_event_id=n*2+1, human_polygon_revision=3,
            human_event_sha256=hashlib.sha256(human_raw.encode()).hexdigest(),
            source_event_history=history, snapshot_sha256='pose-snapshot',
            formal_manifest_sha256='formal', class_assignments_sha256='classes',
            machine_transfer_performed=False, new_human_edit_created=False,
            metrics={}, **dict.fromkeys(original.FALSE_FLAGS, False)))
    current, direct, _ = original.annotation_inventory(db, rows)
    templates = original.seed_union(direct, {}, 'pose-snapshot')
    for record in records:
        record['template'] = original.template_metadata(templates[record['target_key']])
    return records, current, templates


@pytest.fixture
def human_fixture():
    with sqlite3.connect(':memory:') as db:
        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);')
        rows = {}
        records, current, templates = make_supplements(db, rows)
        manual = [dict(target_key='old-manual', class_id=2, source=row('old-manual', 2),
            original_pose_url='https://pose.example/?mode=edit&target=old-manual')]
        manifest = dict(snapshot_sha256='pose-snapshot', formal_manifest_sha256='formal',
            classification_sha256='classes')
        yield dict(db=db, rows=rows, records=records, current=current, templates=templates,
            combined=manual, manual=manual, manifest=manifest)


def supplement(fixture):
    return extension.supplement_proposals(fixture['records'], fixture['templates'], fixture['current'],
        fixture['rows'], fixture['combined'], fixture['manual'], fixture['manifest'], 'diagnostic-sha')


def test_six_existing_saved_human_records_are_not_new_edits_or_requests(human_fixture):
    fixture = human_fixture
    before = copy.deepcopy({k: v for k, v in fixture.items() if k != 'db'})
    changes = fixture['db'].total_changes
    proposals = supplement(fixture)
    assert {k: v for k, v in fixture.items() if k != 'db'} == before
    assert fixture['db'].total_changes == changes
    assert len(proposals) == 6
    assert {p['target_key'] for p in proposals} == extension.SUPPLEMENT_KEYS
    for p in proposals:
        key = p['target_key']
        assert p['schema'] == original.MANUAL_SCHEMA
        assert p['manual_source_kind'] == 'authorized_existing_human_mask_reuse'
        assert not p['new_human_edit_created'] and not p['machine_transfer_performed']
        assert p['pose_snapshot_saved'] and p['human_edited']
        assert not p['pose_store_written'] and not p['gallery_store_written']
        assert not any(p[flag] for flag in original.FALSE_FLAGS)
        assert p['metrics'] == {} and 'manual_request_sha256' not in p
        assert 'production_saved' not in p
        assert p['template'] == original.template_metadata(fixture['templates'][key])
        assert p['template']['target_key'] == key
        assert p['rendering_id'] == 'quota_generation:'+key
        assert p['polygon'] == fixture['current'][key]['polygon']
        assert p['target_expected_revision'] == 9 and p['template']['human_polygon_revision'] == 3
        assert p['original_pose_url'] == 'https://pose.example/?mode=edit&target='+key
    frozen = original.freeze_manual_sources(fixture['db'], proposals)
    extension.check_diagnostic_sources(fixture['db'], fixture['records'], frozen)
    assert frozen[0]['human_event_sha256'] != extension.sha(frozen[0]['human_event_payload'])


@pytest.mark.parametrize('attack', ['missing', 'extra', 'duplicate', 'prior_key', 'prior_frame',
    'class', 'source_identity', 'missing_template', 'machine_origin', 'deleted', 'deferred',
    'polygon', 'payload', 'human_event', 'formal', 'snapshot', 'machine_claim', 'new_edit_claim',
    'metrics', 'confirmed', 'training', 'robot'])
def test_supplement_rejects_invalid_sources(human_fixture, attack):
    f = human_fixture; r = f['records'][0]; key = r['target_key']
    if attack == 'missing':
        f['records'].pop()
    elif attack == 'extra':
        f['records'].append(dict(r, target_key='unauthorized'))
    elif attack == 'duplicate':
        f['records'].append(copy.deepcopy(r))
    elif attack in ('prior_key', 'prior_frame'):
        f['combined'].append(dict(target_key=key if attack == 'prior_key' else 'another',
            source=copy.deepcopy(f['rows'][key])))
    elif attack == 'class':
        r['class_id'] = 2
    elif attack == 'source_identity':
        r['source'] = dict(r['source'], split='test')
    elif attack == 'missing_template':
        del f['templates'][key]
    elif attack == 'machine_origin':
        f['templates'][key]['origin'] = 'machine'
    elif attack in ('deleted', 'deferred'):
        f['current'][key]['state'] = attack
    elif attack == 'polygon':
        r['polygon'][0][0] += .1
    elif attack == 'payload':
        r['current_payload_sha256'] = 'wrong'
    elif attack == 'human_event':
        r['template']['human_event_sha256'] = 'wrong'
    elif attack == 'formal':
        r['formal_manifest_sha256'] = 'wrong'
    elif attack == 'snapshot':
        r['snapshot_sha256'] = 'wrong'
    elif attack == 'machine_claim':
        r['machine_transfer_performed'] = True
    elif attack == 'new_edit_claim':
        r['new_human_edit_created'] = True
    elif attack == 'metrics':
        r['metrics']['boundary_ncc'] = 1
    else:
        r[{'confirmed': 'human_mask_confirmed', 'training': 'automatic_training_eligible',
            'robot': 'robot_command_eligible'}[attack]] = True
    with pytest.raises(ValueError):
        supplement(f)


@pytest.mark.parametrize('attack', ['raw_current', 'raw_current_sha', 'event_id', 'event_sha', 'history'])
def test_diagnostic_raw_current_and_full_event_histories_are_checked(human_fixture, attack):
    f = human_fixture
    frozen = original.freeze_manual_sources(f['db'], supplement(f))
    r = f['records'][0]
    if attack == 'raw_current':
        r['current_payload_raw'] = json.dumps(r['current_payload'])
    elif attack == 'raw_current_sha':
        r['current_payload_raw_sha256'] = 'wrong'
    elif attack == 'event_id':
        r['human_event_id'] += 100
    elif attack == 'event_sha':
        r['human_event_sha256'] = 'wrong'
    else:
        r['source_event_history'].pop()
    with pytest.raises(ValueError, match='raw payload/event history differs'):
        extension.check_diagnostic_sources(f['db'], f['records'], frozen)


def test_only_six_excluded_human_outcomes_are_changed():
    values = [dict(target_key=k, class_id=10, reason='human_mask_history_protected')
        for k in sorted(extension.SUPPLEMENT_KEYS)]
    values.append(dict(target_key='machine', class_id=10, reason='new_proposal'))
    before = copy.deepcopy(values)
    result = extension.extend_outcomes(values, extension.SUPPLEMENT_KEYS)
    assert values == before and len(result) == len(values)
    assert result[-1] == values[-1]
    assert all(v['reason'] == 'saved_human_source_added' for v in result[:-1])
    values[0]['reason'] = 'deleted'
    with pytest.raises(ValueError):
        extension.extend_outcomes(values, extension.SUPPLEMENT_KEYS)


def test_compose_requires_verification_before_creating_output(tmp_path):
    source = tmp_path/'source'; source.mkdir()
    out = tmp_path/'out'
    with pytest.raises(ValueError, match='verification.json is absent'):
        extension.compose(out, source, tmp_path/'not-needed.jsonl')
    assert not out.exists()


@pytest.fixture
def synthetic_generation(tmp_path, monkeypatch):
    """A fake passed seal applies only to synthetic files in pytest's directory."""
    monkeypatch.setattr(extension, 'PROJECT', tmp_path)
    source = tmp_path/'outputs/v1'; source.mkdir(parents=True)
    (tmp_path/'pallet').mkdir()
    (tmp_path/'pallet/mask_quota_completion.py').write_bytes(b'fixture-frozen-generator')
    code_sha = original.digest(tmp_path/'pallet/mask_quota_completion.py')
    for directory in ('images', 'cases', 'masks', 'labelme', 'templates', 'refined/templates',
            'generation/templates', 'quota_generation/templates', 'backups'):
        (source/directory).mkdir(parents=True, exist_ok=True)
    rows, old, manual, new = {}, [], [], []
    for prefix, counts, values in (
            ('old', {1: 48, 2: 5, 9: 38, 10: 10, 15: 39}, old),
            ('manual', {1: 2, 2: 8, 9: 3, 10: 8, 15: 3}, manual),
            ('machine', {2: 37, 9: 9, 10: 13, 15: 8}, new)):
        for cls, count in counts.items():
            for n in range(count):
                key = f'{prefix}-{cls}-{n}'
                rows[key] = r = row(key, cls)
                p = dict(target_key=key, class_id=cls, source=r, polygon=POLYGON, vertex_count=8)
                if prefix == 'manual':
                    p.update(schema=original.MANUAL_SCHEMA, target_expected_revision=9,
                        original_pose_url='https://pose.example/?target='+key)
                values.append(p)
                for folder, ext in (('images', 'png'), ('cases', 'png'), ('masks', 'png'), ('labelme', 'json')):
                    (source/folder/f'{key}.{ext}').write_bytes(b'fixture-frozen-'+key.encode())
    combined = sorted(old+manual+new, key=lambda p: p['target_key'])
    snapshot = source/'reviews_snapshot.sqlite3'
    with sqlite3.connect(snapshot) as db:
        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);')
        records, _, templates = make_supplements(db, rows)
    snapshot.chmod(0o400)
    (source/'gallery_reviews_snapshot.sqlite3').write_bytes(b'fixture-frozen-gallery')
    pose_sha = original.digest(snapshot)
    for t in templates.values():
        t['pose_review']['snapshot_sha256'] = pose_sha
    for r in records:
        r['snapshot_sha256'] = pose_sha
        r['template'] = original.template_metadata(templates[r['target_key']])
    for path in (snapshot, source/'gallery_reviews_snapshot.sqlite3'):
        (source/'backups'/path.name).write_bytes(path.read_bytes())
        (source/'backups'/path.name).chmod(0o400)
    for n in range(169):
        key = f'unused-human-seed-{n}'
        templates[key] = dict(copy.deepcopy(next(iter(templates.values()))), row=row(key))
    template_values = [dict(t, rendering_id='quota_generation:'+key) for key, t in templates.items()]
    template_values.append(dict(copy.deepcopy(template_values[0]), rendering_id='historical:first'))
    for t in template_values:
        if t['rendering_id'].startswith('quota_generation:'):
            original.write_json(source/'quota_generation/templates'/f'{t["row"]["target_key"]}.json', t)
    for name in ('templates', 'refined/templates', 'generation/templates'):
        (source/name/'frozen.json').write_bytes(b'fixture-frozen-template')
    review_counts = {1: 27, 2: 5, 9: 20, 10: 8, 15: 37}
    reviews = {}
    for cls, count in review_counts.items():
        for p in [p for p in old if p['class_id'] == cls][:count]:
            reviews[p['target_key']] = dict(target_key=p['target_key'], polygon=POLYGON, revision=7)
    outcomes = [dict(target_key=p['target_key'], class_id=p['class_id'], reason='new_proposal') for p in new]
    outcomes += [dict(target_key=key, class_id=10, reason='human_mask_history_protected')
        for key in sorted(extension.SUPPLEMENT_KEYS)]
    counts = original.quota_counts(old, new, reviews, manual=manual)
    for c in counts.values():
        c.update(templates=22, eligible=425, compatible_targets=226)
    inventory = dict(templates=template_values, rendering_by_target={p['target_key']: 'historical:first' for p in combined},
        generation_template_keys=sorted(templates), generation_templates=175, classes=counts,
        manual_proposals=24, counted_manual_source_keys=sorted(p['target_key'] for p in manual))
    files = {'proposals': combined, 'manual_proposals': manual, 'new_proposals': new,
        'manual_source_records': [dict(target_key=p['target_key'], fixture='original-24') for p in manual],
        'refined_annotations': list(reviews.values()), 'outcomes': outcomes,
        'requests': [dict(target_key=p['target_key'], status='original_pending_request') for p in manual],
        'registration_attempts': [dict(fixture='frozen original strict attempts')],
        'accepted_candidates': new}
    for name, values in files.items():
        original.write_jsonl(source/f'{name}.jsonl', values)
    original.write_json(source/'template_inventory.json', inventory)
    for name, value in [('protected_formal_before', {'fixture': 'formal-unchanged'}),
            ('protected_formal_after', {'fixture': 'formal-unchanged'}),
            ('protected_targets', {'fixture': 'protected-original-history'}), ('snapshot_manifest', {'fixture': 'snapshots'}),
            ('used_image_hashes', {}), ('preserved_parent_assets', {'fixture': 'parent-assets'})]:
        original.write_json(source/f'{name}.json', value)
    for name in ('report_zh.html', 'report-font.otf', 'GENERATION_HANDOFF.md', 'FONT_LICENSE.txt', 'preview_all_classes.png'):
        (source/name).write_bytes(b'fixture-frozen-report-or-contact')
    m = dict(publication_version=6, version=original.VERSION, proposals=231, previous_proposals=140,
        manual_proposals=24, new_proposals=67, generation_templates=175, classes=counts,
        code_sha256=code_sha, frozen_code_sha256={'pallet/mask_quota_completion.py': code_sha},
        snapshot_sha256=pose_sha, gallery_snapshot_sha256=original.digest(source/'gallery_reviews_snapshot.sqlite3'),
        formal_manifest_sha256='formal', classification_sha256='classes',
        parent='outputs/pub5', parent_proposals_sha256='parent-sha', parent_verification_sha256='parent-verification',
        snapshot_saved=4758, snapshot_events=10290, gallery_snapshot_saved=97, gallery_snapshot_events=104,
        generation_template_namespace='quota_generation')
    for name in files:
        if name in ('proposals', 'manual_proposals', 'new_proposals', 'manual_source_records', 'requests'):
            m[name+'_sha256'] = original.digest(source/f'{name}.jsonl')
    m['template_inventory_sha256'] = original.digest(source/'template_inventory.json')
    original.write_json(source/'manifest.json', m)
    original.write_json(source/'artifact_hashes.json', extension.file_inventory(source))
    # Synthetic seal, never written into or used to bypass the actual production v1.
    original.write_json(source/'verification.json', dict(state='passed', publication_version=6,
        proposals_verified=231, manifest_sha256=original.digest(source/'manifest.json'),
        proposals_sha256=m['proposals_sha256']))
    diagnostic = tmp_path/'outputs/diagnostic/existing_human_reuse.jsonl'
    diagnostic.parent.mkdir()
    original.write_jsonl(diagnostic, records)
    formal = tmp_path/'formal'; formal.mkdir()
    evidence = type('Evidence', (), {'image': lambda self, frame: np.full((16, 24), 100, np.uint8), 'used': {}})()
    monkeypatch.setattr(extension, 'load_formal_evidence', lambda manifest: (formal, rows, evidence))
    monkeypatch.setattr(extension, 'protected_snapshot', lambda formal: {'fixture': 'formal-unchanged'})
    live_checks = []
    monkeypatch.setattr(extension, 'assert_live_sources_unchanged', lambda frozen: live_checks.append(copy.deepcopy(frozen)))
    return dict(source=source, output=tmp_path/'outputs/v2', diagnostic=diagnostic,
        manual=manual, old=old, new=new, rows=rows, templates=template_values, live_checks=live_checks)


def test_compose_preserves_frozen_generation_and_adds_only_six_humans(synthetic_generation):
    f = synthetic_generation; source, out = f['source'], f['output']
    before = extension.file_inventory(source)
    result = extension.compose(out, source, f['diagnostic'])
    assert extension.file_inventory(source) == before
    assert result['publication_version'] == 6 and result['proposals'] == 237
    assert result['manual_proposals'] == 30 and result['new_proposals'] == 67
    assert result['code_sha256'] == extension.read_json(source/'manifest.json')['code_sha256']
    assert result['extension_code_sha256'] == original.digest(extension.__file__)
    assert result['source_generation'] == 'outputs/v1'
    assert result['source_generation_manifest_sha256'] == before['manifest.json']
    assert result['source_generation_verification_sha256'] == before['verification.json']
    assert result['source_generation_proposals_sha256'] == before['proposals.jsonl']
    assert result['base_proposals'] == 170
    assert result['classes'][10]['total'] == 37 and result['classes'][10]['missing'] == 13
    assert result['classes'][10]['manual_added'] == result['classes'][10]['saved_pose_human'] == 14
    assert result['classes'][10]['prior_machine_pending'] == 2
    assert sum(c['saved_human'] for c in result['classes'].values()) == 127
    assert {c: v['total'] for c, v in result['classes'].items()} == {1: 50, 2: 50, 9: 50, 10: 37, 15: 50}
    assert result['research_candidates_added'] == 0 and result['new_machine_proposals_generated_by_extension'] == 0
    combined = extension.indexed(extension.read_jsonl(out/'proposals.jsonl'))
    for p in extension.read_jsonl(source/'proposals.jsonl'):
        assert original.canonical(combined[p['target_key']]) == original.canonical(p)
    assert len(combined) == len({p['source']['frame_id'] for p in combined.values()}) == 237
    for name in ('requests.jsonl', 'new_proposals.jsonl', 'registration_attempts.jsonl', 'accepted_candidates.jsonl',
            'reviews_snapshot.sqlite3', 'gallery_reviews_snapshot.sqlite3', 'protected_formal_before.json',
            'protected_formal_after.json', 'preview_all_classes.png'):
        assert (out/name).read_bytes() == (source/name).read_bytes()
    assert (out/'reviews_snapshot.sqlite3').stat().st_mode & 0o777 == 0o400
    assert not (out/'verification.json').exists()
    assert (out/'source_generation_evidence/verification.json').read_bytes() == (source/'verification.json').read_bytes()
    assert len(extension.read_jsonl(out/'requests.jsonl')) == 24
    assert len(extension.read_jsonl(out/'manual_source_records.jsonl')) == 30
    assert len(extension.read_jsonl(out/'manual_supplement_source_records.jsonl')) == 6
    inventory = extension.read_json(out/'template_inventory.json')
    assert inventory['templates'] == f['templates']
    assert len(inventory['templates']) == 176  # 175 generation plus one historical namespace
    for key in extension.SUPPLEMENT_KEYS:
        assert inventory['rendering_by_target'][key] == 'quota_generation:'+key
        label = extension.read_json(out/'labelme'/f'{key}.json')
        assert label['flags']['machine_draft'] is False and label['flags']['human_edited'] is True
    hashes = extension.read_json(out/'artifact_hashes.json')
    assert all(original.digest(out/name) == value for name, value in hashes.items())
    preserved = extension.read_json(out/'preserved_source_generation_assets.json')
    assert all(original.digest(out/name) == value == before[name] for name, value in preserved.items())
    assert len(f['live_checks']) == 2
    page = (out/'report_zh.html').read_text()
    assert '既有人工轮廓复用 r9' in page and '不是本轮新描绘，不是机器迁移' in page
    assert '仍缺13帧' in page and '并未完成250帧目标' in page
    assert '原队列24份人工描绘保持不变' in page


@pytest.mark.parametrize('attack', ['seal_failed', 'seal_publication', 'seal_manifest', 'seal_proposals',
    'source_proposals', 'generator_code', 'snapshot', 'artifact'])
def test_source_verification_and_frozen_hashes_are_required(synthetic_generation, attack):
    f = synthetic_generation; source = f['source']
    if attack.startswith('seal_'):
        seal = extension.read_json(source/'verification.json')
        field, value = {'seal_failed': ('state', 'failed'), 'seal_publication': ('publication_version', 7),
            'seal_manifest': ('manifest_sha256', 'wrong'), 'seal_proposals': ('proposals_sha256', 'wrong')}[attack]
        seal[field] = value
        original.write_json(source/'verification.json', seal)
    elif attack == 'source_proposals':
        (source/'proposals.jsonl').write_bytes(b'changed')
    elif attack == 'generator_code':
        (extension.PROJECT/'pallet/mask_quota_completion.py').write_bytes(b'changed')
    elif attack == 'snapshot':
        (source/'gallery_reviews_snapshot.sqlite3').write_bytes(b'changed')
    else:
        (source/'requests.jsonl').write_bytes(b'changed')
    with pytest.raises(ValueError):
        extension.compose(f['output'], source, f['diagnostic'])
    assert not f['output'].exists()


def test_live_source_check_is_readonly_and_detects_new_revision(tmp_path, human_fixture, monkeypatch):
    f = human_fixture
    frozen = original.freeze_manual_sources(f['db'], supplement(f))
    path = tmp_path/'outputs/pallet_pose_human_review_v1/annotations.sqlite3'
    path.parent.mkdir(parents=True)
    with sqlite3.connect(path) as target:
        f['db'].commit()
        f['db'].backup(target)
    monkeypatch.setattr(extension, 'PROJECT', tmp_path)
    before = path.read_bytes()
    extension.assert_live_sources_unchanged(frozen)
    assert path.read_bytes() == before
    with sqlite3.connect(path) as db:
        db.execute('UPDATE annotations SET revision=revision+1 WHERE target_key=?', (frozen[0]['target_key'],))
    with pytest.raises(ValueError, match='changed in the live pose store'):
        extension.assert_live_sources_unchanged(frozen)
