"""Independent publication-13 policy attacks; no generator or production writes."""
import ast
from collections import Counter
import copy
import json
import sqlite3

import pytest

from scripts import verify_mask_remaining_round3 as audit


FOUR = [[20, 20], [85, 21], [83, 43], [19, 42]]
FINE = [[20, 20], [85, 20], [85, 44], [65, 44], [65, 35], [40, 35], [40, 44], [20, 44]]


def row(key='human', cls=4, frame=None):
    return dict(target_key=key, target_id=key+'::box:0000', frame_id=frame or 'frame-'+key,
        final_class_id=cls, split='train', group_id='group', box_index=0,
        bbox=dict(x=18, y=18, width=70, height=28), size=[120, 80], policy_id='camera',
        source_pcd_sha256='pcd', image='images/'+key+'.png', viewpoint_candidate='front')


def value(r, polygon=FOUR, revision=1, **extra):
    return dict(target_key=r['target_key'], revision=revision, source=copy.deepcopy(r),
        state='draft', polygon=copy.deepcopy(polygon), **extra)


def event(v, event_id=None):
    return (event_id or v['revision'], v['target_key'], v['revision'], json.dumps(v, indent=2, ensure_ascii=False))


def store(current=None, events=(), metadata=None):
    return dict(path=audit.Path('readonly.sqlite3'), metadata=metadata or {}, current=current or {},
        events=list(events), history_keys={e[1] for e in events})


def proposal(r, polygon=FOUR):
    return dict(target_key=r['target_key'], source=r, class_id=r['final_class_id'], polygon=copy.deepcopy(polygon), vertex_count=len(polygon))


def snapshot(path, kind, events, metadata=None):
    table = 'annotations' if kind == 'pose' else 'reviews'
    latest = {key: (revision, raw) for _, key, revision, raw in events}
    with sqlite3.connect(path) as db:
        db.executescript(f'CREATE TABLE {table}(target_key TEXT,revision INTEGER,payload TEXT);'
            'CREATE TABLE events(id INTEGER,target_key TEXT,revision INTEGER,payload TEXT);'
            'CREATE TABLE metadata(key TEXT,value TEXT);')
        db.executemany(f'INSERT INTO {table} VALUES (?,?,?)', ((k, *v) for k, v in latest.items()))
        db.executemany('INSERT INTO events VALUES (?,?,?,?)', events)
        db.executemany('INSERT INTO metadata VALUES (?,?)', (metadata or {}).items())
    return audit.helpers.read_store(path, kind, frozen=True)


def honest_manifest():
    return dict(publication_version=13, method='original', target_per_class=100,
        generation_template_namespace=audit.NAMESPACE, production_database_writes=0,
        configuration_writes=0, service_writes=0, new_human_edits_created=0,
        thresholds_relaxed=False, forced_fill=False, recursive_candidate_propagation=False,
        automatic_human_confirmation=False, automatic_training_eligible=False,
        robot_command_eligible=False, ground_truth_eligible=False,
        original_splits_preserved=True, classification_is_candidate_not_truth=True)


def test_actual_publication13_policy_never_rebinds_old18554_statement_to_current50():
    assert audit.PUBLICATION == 13 and audit.NAMESPACE == 'remaining_round3_generation'
    assert sum(audit.BASE_COUNTS.values()) == 1449
    assert len(audit.ORIGINAL_REVIEWS) == 5 and len(audit.PENDING_RECTANGLES) == 2
    assert audit.previous.PUBLICATION == 12 and audit.previous.USER_STATEMENT != audit.USER_STATEMENT
    assert '其中没有修改记录' not in audit.USER_STATEMENT
    assert set(audit.COMPLETED) == {1, 2, 3, 6, 9, 10, 13, 15}


@pytest.mark.parametrize('field,bad', [('publication_version', 12), ('method', 'research'), ('target_per_class', 99),
    ('generation_template_namespace', 'remaining_round2_generation'), ('production_database_writes', False),
    ('configuration_writes', 1), ('service_writes', '0'), ('new_human_edits_created', 1), ('thresholds_relaxed', True),
    ('forced_fill', True), ('recursive_candidate_propagation', True), ('automatic_human_confirmation', True),
    ('automatic_training_eligible', True), ('robot_command_eligible', True), ('ground_truth_eligible', True),
    ('human_confirmation_inferred', True), ('published', True), ('human_requests_created', 41)])
def test_manifest_rejects_writes_relaxed_gates_drafts_as_requests_and_truthy_zero(field, bad):
    manifest = honest_manifest()
    audit.check_claims(manifest)
    manifest[field] = bad
    with pytest.raises(ValueError):
        audit.check_claims(manifest)


@pytest.fixture
def partition():
    requests = {f'task-{c}-{i}': dict(target_key=f'task-{c}-{i}', class_id=c, source=row(f'task-{c}-{i}', c))
        for c in (4, 5, 7, 14, 22) for i in range(10)}
    for key, (cls, _) in audit.PENDING_RECTANGLES.items():
        requests.pop(f'task-{cls}-0')
        requests[key] = dict(target_key=key, class_id=cls, source=row(key, cls))
    manual = {k: proposal(r['source']) for k, r in requests.items() if k not in audit.PENDING_RECTANGLES}
    manual_counts = Counter(p['class_id'] for p in manual.values())
    old = {f'old-{c}-{i}': proposal(row(f'old-{c}-{i}', c)) for c, n in audit.BASE_COUNTS.items()
        for i in range(n-manual_counts[c])}
    for i, (key, (cls, _)) in enumerate(audit.ORIGINAL_REVIEWS.items()):
        old.pop(f'old-{cls}-{i}')
        old[key] = dict(proposal(row(key, cls)), schema=audit.previous.REVIEW_SCHEMA)
    new = {'machine': proposal(row('machine', 4), FINE)}
    combined = copy.deepcopy({**old, **manual, **new})
    return old, requests, manual, new, combined, copy.deepcopy(new)


def test_exact1401_plus48_plus_n_never_counts_two_pending_rectangles(partition):
    counts = audit.check_partition(*partition)
    assert len(partition[4]) == 1450 and counts['4']['proposals'] == 36
    assert not set(audit.PENDING_RECTANGLES) & set(partition[4])


@pytest.mark.parametrize('attack', ['count_pending', 'reuse_reserved_frame', 'lost_old', 'rewrite_old', 'missing_manual',
    'fake_extra_manual', 'lost_old_review', 'new_review_as_edit', 'machine_completed', 'accepted_manual', 'accepted_rewrite',
    'frame_reuse', 'crossclass_frame', 'overquota', 'assistant_draft'])
def test_partition_rejects_source_laundering_reserve_quota_fill_and_draft_contamination(partition, attack):
    old, requests, manual, new, combined, accepted = partition
    key = next(iter(audit.PENDING_RECTANGLES))
    if attack == 'count_pending':
        manual[key] = combined[key] = proposal(requests[key]['source'])
    elif attack == 'reuse_reserved_frame':
        for d in (new, combined, accepted):
            d['machine']['source']['frame_id'] = requests[key]['source']['frame_id']
    elif attack == 'lost_old':
        combined.pop(next(iter(old)))
    elif attack == 'rewrite_old':
        combined[next(iter(old))]['comment'] = 'modified'
    elif attack == 'missing_manual':
        manual.pop(next(iter(manual)))
    elif attack == 'fake_extra_manual':
        manual['extra'] = proposal(row('extra'))
    elif attack == 'lost_old_review':
        key = next(iter(audit.ORIGINAL_REVIEWS)); old[key].pop('schema'); combined[key] = copy.deepcopy(old[key])
    elif attack == 'new_review_as_edit':
        manual[next(iter(manual))]['schema'] = audit.previous.REVIEW_SCHEMA
    elif attack == 'machine_completed':
        for d in (new, combined, accepted):
            d['machine']['class_id'] = 3
    elif attack == 'accepted_manual':
        key = next(iter(manual)); accepted[key] = copy.deepcopy(manual[key])
    elif attack == 'accepted_rewrite':
        accepted['machine']['polygon'].reverse()
    elif attack == 'frame_reuse':
        for d in (new, combined, accepted):
            d['machine']['source']['frame_id'] = next(iter(old.values()))['source']['frame_id']
    elif attack == 'crossclass_frame':
        manual['task-5-1']['source']['frame_id'] = manual['task-7-1']['source']['frame_id']
        combined['task-5-1'] = copy.deepcopy(manual['task-5-1'])
    elif attack == 'assistant_draft':
        combined['assistant'] = proposal(row('assistant'))
    else:
        for i in range(100):
            key = 'extra'+str(i); new[key] = combined[key] = accepted[key] = proposal(row(key), FINE)
    with pytest.raises(ValueError):
        audit.check_partition(*partition)


@pytest.fixture
def pending(tmp_path):
    binding = dict(formal_manifest_sha256='formal')
    events, rows = [], {}
    for i, (key, (cls, revision)) in enumerate(audit.PENDING_RECTANGLES.items(), 1):
        r = rows[key] = row(key, cls)
        events.append(event(value(r, audit.sources.rectangle(r), revision, binding=audit.sha(binding)), i))
    path = tmp_path/'pose.sqlite3'
    pose = snapshot(path, 'pose', events, {'binding': json.dumps(binding)})
    actual = {k: audit.saved_rectangle_source(k, pose, r, path) for k, r in rows.items()}
    records = {k: dict(v, snapshot_sha256='snapshot', human_edited=False, propagation_seed_eligible=False,
        counts_toward_published_coverage=False, user_reviewed_no_change=False) for k, v in actual.items()}
    return actual, records, pose, rows, path


def test_pending_original_rectangles_bind_real_saved_utf8_without_inventing_inspection(pending):
    actual, records, pose, rows, _ = pending
    audit.check_pending_records(records, actual, 'snapshot')
    assert audit.sources.recover_pose_sources(pose, rows) == {}
    for source in actual.values():
        assert source['saved_event_raw_sha256'] == audit.raw_sha(source['saved_event_payload_raw'])
        assert 'human_polygon_revision' not in source


@pytest.mark.parametrize('field,bad', [('human_edited', True), ('propagation_seed_eligible', True),
    ('counts_toward_published_coverage', True), ('user_reviewed_no_change', True), ('user_statement_sha256', 'old18554'),
    ('human_polygon_revision', 1), ('human_event_id', 1), ('human_event_sha256', 'invented'), ('template', {}),
    ('current_payload_raw', 'JSON-equivalent is not raw'), ('saved_event_raw_sha256', 'canonical-not-raw'),
    ('ground_truth_eligible', True)])
def test_pending_records_cannot_turn_saved_rectangle_into_reviewed_edit_seed_or_goal(pending, field, bad):
    actual, records, *_ = pending
    records[next(iter(records))][field] = bad
    with pytest.raises(ValueError):
        audit.check_pending_records(records, actual, 'snapshot')


def test_returned_rectangle_with_old_mask_edit_still_protects_whole_frame(pending):
    _, _, pose, rows, path = pending
    key = next(iter(rows))
    pose['events'].insert(0, event(value(rows[key], FOUR, binding=audit.sha(json.loads(pose['metadata']['binding']))), 500))
    audit.saved_rectangle_source(key, pose, rows[key], path)
    sibling = row('sibling', 19, rows[key]['frame_id']); rows['sibling'] = sibling
    keys, frames = audit.protection(rows, pose, store(), {}, set())
    assert key in keys and sibling['frame_id'] in frames


@pytest.fixture
def manual():
    r = row(); v = value(r)
    recovered = audit.sources.recover_pose_sources(store({'human': v}, [event(v)]), {'human': r})
    s = recovered['human']
    task = dict(target_key='human', class_id=4, source=copy.deepcopy(r), pose_annotation_url='https://pose.example/?target=human')
    p = dict(proposal(r), schema='saved_pose_mask_gallery_entry_v1', origin=audit.POSE_ORIGIN,
        status='human_edited_draft_pending_confirmation', human_edited=True, manual_four_point_source=True,
        propagation_seed_eligible=False, pose_store_written=False, gallery_store_written=False,
        machine_transfer_performed=False, new_human_edit_created=False, snapshot_sha256='snapshot',
        formal_manifest_sha256='formal', class_assignments_sha256='classes', target_expected_revision=1,
        target_snapshot_payload_sha256=s['current_payload_sha256'], metrics={}, rendering_id=audit.NAMESPACE+':manual_source:human',
        manual_request_sha256=audit.sha(task), original_pose_url=task['pose_annotation_url'],
        human_source=audit.template_binding(audit.matching.pose_template(s, 'snapshot')))
    return p, task, r, recovered, 'snapshot', 'formal', 'classes'


def test_actual_changed_quad_keeps_four_vertices_and_exact_event_binding(manual):
    audit.check_manual(*manual)


@pytest.mark.parametrize('attack', ['vertices', 'seed', 'human_event', 'current_revision', 'stale_human', 'request',
    'split', 'view', 'confirmed', 'write', 'metric', 'original_rectangle'])
def test_modified_quad_cannot_invent_points_source_event_or_confirmation(manual, attack):
    p, task, r, recovered, *_ = manual
    if attack == 'vertices': p['polygon'].append([30, 30])
    elif attack == 'seed': p['propagation_seed_eligible'] = True
    elif attack == 'human_event': p['human_source']['human_event_sha256'] = 'canonical-not-raw'
    elif attack == 'current_revision': p['target_expected_revision'] = 0
    elif attack == 'stale_human': recovered['human']['human_polygon_revision'] = 0
    elif attack == 'request': p['manual_request_sha256'] = 'fake'
    elif attack in ('split', 'view'): task['source']['split' if attack == 'split' else 'viewpoint_candidate'] = 'other'
    elif attack == 'confirmed': p['ground_truth_eligible'] = True
    elif attack == 'write': p['pose_store_written'] = True
    elif attack == 'metric': p['metrics']['ecc'] = 1
    else: recovered.clear()
    with pytest.raises(ValueError):
        audit.check_manual(*manual)


def test_seed_set_is_parent599_plus_saved51_including_saved_unchanged_never_recursive_machine():
    recovered = {}
    for key, cls, polygon in [('pose', 4, FINE), ('quad', 4, FOUR), ('oldfive', 1, FINE)]:
        r = row(key, cls); v = value(r, polygon)
        recovered.update(audit.sources.recover_pose_sources(store({key: v}, [event(v)]), {key: r}))
    galleries = {k: dict(row=row(k, c), polygon=FINE, origin=audit.GALLERY_ORIGIN)
        for k, c in [('oldgallery', 4), ('saved-unchanged', 5), ('old-five-gallery', 1)]}
    result = audit.independent_seeds(recovered, recovered, galleries, {'pose', 'oldgallery'}, {'saved-unchanged': {}}, 'snapshot')
    assert set(result) == {'pose', 'oldgallery', 'saved-unchanged'}
    galleries['unreviewed-machine'] = dict(row=row('unreviewed-machine'), polygon=FINE, origin=audit.GALLERY_ORIGIN)
    with pytest.raises(ValueError, match='exact parent599'):
        audit.independent_seeds(recovered, recovered, galleries, {'pose', 'oldgallery'}, {'saved-unchanged': {}}, 'snapshot')


class Evidence:
    def patch(self, key): return key


@pytest.fixture
def attempts(monkeypatch):
    rows = {k: row(k) for k in ('selected', 'unselected', 'rejected', 'low')}
    templates = {k: dict(row=row(k), polygon=copy.deepcopy(FINE)) for k in ('best', 'second', 'below')}
    scores = {'best': .8, 'second': .7, 'below': .549999}
    monkeypatch.setattr(audit, 'correlate', lambda source, target: .549999 if target == 'low' else scores[source])
    metrics = dict(initial_ncc=.7, ecc=.84, boundary_ncc=.78, forward_backward_error_patch_pixels=[1.5]*8,
        boundary_guard=dict(state='passed', all_beams=[True, 2, .1]))
    calls = []
    def replay(template, target, evidence):
        source, key = template['row']['target_key'], target['target_key']; calls.append((source, key))
        if source == 'second' and key in ('selected', 'unselected'): return copy.deepcopy(FINE), copy.deepcopy(metrics)
        return None, dict(rejection='image_registration_failed', initial_ncc=scores[source])
    monkeypatch.setattr(audit, 'replay', replay)
    trials = [dict(target_key=k, class_id=4, template_key=s, metrics=replay(templates[s], rows[k], None)[1])
        for k in ('selected', 'unselected', 'rejected') for s in ('best', 'second')]
    calls.clear()
    accepted = {k: dict(proposal(rows[k], FINE), template=dict(target_key='second'), metrics=copy.deepcopy(metrics),
        continuation=dict(batch=13, template_rank=2, all_compatible_templates_considered=True, recursive_candidate_propagation=False))
        for k in ('selected', 'unselected')}
    outcomes = {k: dict(target_key=k, class_id=4, reason=reason) for k, reason in [
        ('selected', 'new_proposal'), ('unselected', 'accepted_duplicate_frame_not_selected'),
        ('rejected', 'registration_or_boundary_rejected'), ('low', 'image_similarity_below_gate')]}
    return trials, rows, templates, accepted, outcomes, Evidence(), calls


def test_full_replay_includes_every_rejection_and_accepted_but_unselected(attempts):
    assert audit.audit_attempts(*attempts[:6]) == set(attempts[1])
    assert len(attempts[6]) == 6


@pytest.mark.parametrize('attack', ['missing_rejection', 'omit_unselected', 'wrong_order', 'wrong_rank', 'rejected_metric',
    'unselected_metric', 'vertex_order', 'relaxed_ecc', 'relaxed_boundary', 'relaxed_fb', 'beam',
    'reserved_key', 'reserved_frame', 'filled3', 'filled6', 'filled13', 'missing_outcome', 'cross_split', 'policy', 'view'])
def test_full_replay_rejects_omissions_gate_relaxation_or_unsafe_transfers(attempts, monkeypatch, attack):
    trials, rows, templates, accepted, outcomes, evidence, _ = attempts
    extra = {}
    if attack == 'missing_rejection': trials.pop(4)
    elif attack == 'omit_unselected': trials[:] = [t for t in trials if t['target_key'] != 'unselected']
    elif attack == 'wrong_order': trials[0], trials[1] = trials[1], trials[0]
    elif attack == 'wrong_rank': accepted['selected']['continuation']['template_rank'] = 1
    elif attack == 'rejected_metric': trials[4]['metrics']['initial_ncc'] += .01
    elif attack == 'unselected_metric': accepted['unselected']['metrics']['ecc'] += .01
    elif attack == 'vertex_order': accepted['selected']['polygon'].reverse()
    elif attack.startswith('relaxed_') or attack == 'beam':
        field, bad = {'relaxed_ecc': ('ecc', .83999), 'relaxed_boundary': ('boundary_ncc', .77999),
            'relaxed_fb': ('forward_backward_error_patch_pixels', [1.50001]*8), 'beam': ('boundary_guard', dict(state='review_required'))}[attack]
        accepted['selected']['metrics'][field] = bad; trials[1]['metrics'][field] = bad
        saved = audit.replay
        def bad_replay(template, target, evidence):
            polygon, metrics = saved(template, target, evidence)
            if polygon is not None and target['target_key'] == 'selected': metrics[field] = bad
            return polygon, metrics
        monkeypatch.setattr(audit, 'replay', bad_replay)
    elif attack == 'reserved_key': extra['protected_keys'] = {'selected'}
    elif attack == 'reserved_frame': extra['protected_frames'] = {'frame-selected'}
    elif attack.startswith('filled'):
        cls = int(attack[6:]); rows['selected']['final_class_id'] = outcomes['selected']['class_id'] = cls
        for trial in trials[:2]: trial['class_id'] = cls
    elif attack == 'missing_outcome': outcomes.pop('low')
    else: templates['second']['row'][{'cross_split': 'split', 'policy': 'policy_id', 'view': 'viewpoint_candidate'}[attack]] = 'other'
    with pytest.raises((ValueError, AssertionError)):
        audit.audit_attempts(trials, rows, templates, accepted, outcomes, evidence, **extra)


@pytest.fixture
def seal_output(tmp_path, monkeypatch):
    project = tmp_path/'project'; output = project/'outputs/pallet_mask_remaining_round3_v1'
    output.mkdir(parents=True); (output/'manifest.json').write_text('{}')
    monkeypatch.setattr(audit, 'PROJECT', project)
    return output


def test_failed_audit_writes_only_failed_report_and_keeps_prior_failure_raw(seal_output):
    def fail(_): raise ValueError('invalid human source')
    for _ in range(2):
        prior = (seal_output/audit.REPORT).read_text() if (seal_output/audit.REPORT).exists() else None
        with pytest.raises(ValueError, match='invalid human'):
            audit.seal(seal_output, fail)
        result = audit.read_json(seal_output/audit.REPORT)
        if prior is not None:
            assert result['previous_failed_validation_report_raw'] == prior
            assert result['previous_failed_validation_report_sha256'] == audit.raw_sha(prior)
    assert not (seal_output/audit.SEAL).exists()
    assert {p.name for p in seal_output.iterdir()} == {'manifest.json', audit.REPORT}


def test_success_seal_is_exclusive_and_report_hash_bound(seal_output):
    result = audit.seal(seal_output, lambda _: dict(state='passed', publication_version=13, proposals_verified=1449))
    raw = (seal_output/audit.SEAL).read_bytes()
    assert result['validation_report_sha256'] == audit.digest(seal_output/audit.REPORT)
    with pytest.raises(ValueError, match='overwrite'): audit.seal(seal_output, lambda _: dict(state='passed'))
    assert (seal_output/audit.SEAL).read_bytes() == raw


def test_unsafe_output_report_and_missing_completion_are_rejected(seal_output):
    link = seal_output.parent/'pallet_mask_remaining_round3_v2'; link.symlink_to(seal_output, target_is_directory=True)
    with pytest.raises(ValueError, match='Only a new'): audit.seal(link, lambda _: dict(state='passed'))
    protected = seal_output.parent/'private.json'; protected.write_text('untouched')
    (seal_output/audit.REPORT).symlink_to(protected)
    with pytest.raises(ValueError, match='Unsafe'): audit.seal(seal_output, lambda _: dict(state='passed'))
    assert protected.read_text() == 'untouched'
    (seal_output/'manifest.json').unlink()
    with pytest.raises(ValueError, match='not complete'): audit.seal(seal_output, lambda _: dict(state='passed'))


def test_no_generator_import_live_writer_polling_assert_or_old_module_mutation():
    tree = ast.parse(audit.Path(audit.__file__).read_text())
    for node in ast.walk(tree):
        assert not isinstance(node, ast.Assert)
        if isinstance(node, ast.ImportFrom): assert node.module != 'pallet.mask_remaining_round3'
        if isinstance(node, ast.Import): assert 'pallet.mask_remaining_round3' not in {n.name for n in node.names}
        if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
            assert node.func.attr not in ('backup', 'generate', 'save_review', 'save_annotation', 'setattr', 'sleep')
        if isinstance(node, (ast.Assign, ast.AnnAssign, ast.AugAssign)):
            for target in node.targets if isinstance(node, ast.Assign) else [node.target]:
                assert not (isinstance(target, ast.Attribute) and isinstance(target.value, ast.Name)
                    and target.value.id in ('previous', 'history', 'matching', 'sources', 'helpers', 'baseline'))
