"""Synthetic third-batch tests: two sealed task registries, raw saves and caps."""
import ast
import copy
import json
from pathlib import Path

import pytest

from scripts import verify_mask_under50_requests_v3 as audit
from test_verify_mask_under50_requests import staged, public_pack, draft, export, write, row, POLYGON, KEY
from test_verify_mask_under50_requests_v2 import catalog_case, gallery, event, refreshed_count_fixture


def test_no_producer_import_or_old_verifier_global_mutation_or_live_editor_save():
    tree = ast.parse(Path(audit.__file__).read_text())
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom):
            assert 'build_mask' not in (node.module or '') and 'mask_review_continuation' not in (node.module or '')
            assert not any('build_mask' in value.name or value.name == 'mask_review_continuation' for value in node.names)
        if isinstance(node, ast.Assign):
            assert not any(isinstance(target, ast.Attribute) and isinstance(target.value, ast.Name)
                and target.value.id in ('geometry', 'continuation', 'sources') for target in node.targets)
        if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
            assert node.func.attr not in ('save_review', 'save_annotation', 'backup')


@pytest.fixture
def sealed_roots(tmp_path, monkeypatch):
    outputs = tmp_path/'outputs'; pins = {}
    for number in (1, 2):
        namespace = f'pallet_mask_under50_requests_v{number}'; root = outputs/namespace
        write(root/'manifest.json', dict(number=number))
        write(root/'verification.json', dict(state='passed', number=number))
        write(root/'independent_verification.json', dict(state='passed', number=number))
        pins[namespace] = dict(manifest_sha256=audit.digest(root/'manifest.json'), verification_sha256=audit.digest(root/'verification.json'))
    monkeypatch.setattr(audit, 'SEALED_PACK_PINS', pins)
    own = outputs/'pallet_mask_under50_requests_v3'
    return outputs, own, pins


def test_generic_registry_discovery_loads_both_pinned_sealed_prior_packs_and_not_own(sealed_roots):
    outputs, own, pins = sealed_roots
    write(own/'verification.json', dict(current_partial=True))
    discovered = audit.discover_sealed_packs(outputs, own)
    assert set(discovered) == set(pins) and own not in discovered.values()


@pytest.mark.parametrize('attack', ['missing', 'half_seal', 'wrong_manifest', 'different_twins', 'unknown_sealed'])
def test_prior_registry_refuses_missing_half_sealed_changed_or_unpinned_packages(sealed_roots, attack):
    outputs, own, _ = sealed_roots; old = outputs/'pallet_mask_under50_requests_v2'
    if attack == 'missing':
        (old/'verification.json').unlink(); (old/'independent_verification.json').unlink()
    elif attack == 'half_seal':
        (old/'independent_verification.json').unlink()
    elif attack == 'wrong_manifest':
        write(old/'manifest.json', dict(changed=True))
    elif attack == 'different_twins':
        write(old/'independent_verification.json', dict(changed=True))
    else:
        new = outputs/'pallet_mask_under50_requests_v4'
        for name in audit.SEALS:
            write(new/name, dict(state='passed'))
    with pytest.raises(ValueError):
        audit.discover_sealed_packs(outputs, own)


@pytest.fixture
def completed_batches(tmp_path):
    names = list(audit.SEALED_PACK_PINS)
    task_keys = {names[0]: {f'{n+101:024x}' for n in range(10)}, names[1]: {f'{n+201:024x}' for n in range(9)}}
    baseline_values = [event(f'{n+1:024x}') for n in range(14)]
    additions = [event(f'{n+1:024x}', revision=2, marker='old v3 actual revision') for n in range(14)]
    additions += [event(f'{n+1:024x}', revision=3, marker='old v3 further revision') for n in range(3)]
    registry, refs, namespace_of_key = {}, {}, {}
    for namespace, keys in task_keys.items():
        for key in sorted(keys):
            namespace_of_key[key] = namespace
            proposal = dict(target_key=key, polygon=[[x+1, y] for x, y in POLYGON])
            value = event(key, marker='genuine first save')
            value.update(base_proposal_id=audit.sha(proposal), proposal_publication=15,
                human_edited=True, event_kind='human_mask_gallery_revision')
            additions.append(value)
            registry[15, audit.sha(proposal), key] = dict(kind='independent_preannotation', namespace=namespace, proposal=proposal)
    for key in sorted(task_keys[names[1]])[:2]:
        value = copy.deepcopy(next(value for value in additions if value['target_key'] == key))
        value['revision'] = 2; value['polygon'] = [[x+.5, y] for x, y in POLYGON]
        additions.append(value)
    baseline = gallery(tmp_path/'baseline.sqlite3', baseline_values)
    fresh = gallery(tmp_path/'current.sqlite3', baseline_values+additions)
    for event_id, key, _, raw in fresh['events']:
        value = audit.sources.loads(raw)
        refs[event_id] = registry[15, value['base_proposal_id'], key] if key in namespace_of_key else dict(namespace='v3')
    return fresh, baseline, task_keys, registry, refs


def test19_real_changed_saved_images_have21_save_events_and_not21_distinct_sources(completed_batches):
    extension, completed = audit.check_saved_batches(*completed_batches)
    assert len(extension['new_distinct_saved_keys']) == 19
    assert extension['appended_gallery_events'] == 38 and len(extension['changed_baseline_current_keys']) == 14
    assert list(completed.values()) == [10, 9]
    assert extension['baseline_gallery_current_unchanged'] is False


@pytest.mark.parametrize('attack', ['old_prefix_bytes', 'unregistered_new_key', 'missing_v2_save', 'wrong_namespace',
    'wrong_proposal_hash', 'copy_latest_draft', 'nonhuman_event', 'old_revision_lost', 'wrong_first_revision', 'wrong_route'])
def test_real_prior_saves_need_all_raw_history_exact_package_triples_and_changed_latest_polygons(completed_batches, attack):
    fresh, baseline, tasks, registry, refs = copy.deepcopy(completed_batches)
    namespace = list(tasks)[1]; key = sorted(tasks[namespace])[0]; history = fresh['histories'][key]
    if attack == 'old_prefix_bytes':
        entry = list(fresh['events'][0]); entry[3] = json.dumps(json.loads(entry[3])); fresh['events'][0] = tuple(entry)
    elif attack == 'unregistered_new_key':
        fresh['current']['unregistered'] = fresh['current'][key]
        fresh['histories']['unregistered'] = history
    elif attack == 'missing_v2_save':
        fresh['current'].pop(key); fresh['histories'].pop(key)
    elif attack == 'wrong_namespace':
        refs[history[-1][0]]['namespace'] = list(tasks)[0]
    elif attack == 'wrong_proposal_hash':
        history[-1][3]['base_proposal_id'] = 'bad'
    elif attack == 'copy_latest_draft':
        history[-1][3]['polygon'] = refs[history[-1][0]]['proposal']['polygon']
        fresh['current'][key][2]['polygon'] = refs[history[-1][0]]['proposal']['polygon']
    elif attack == 'nonhuman_event':
        history[-1][3]['human_edited'] = False
    elif attack == 'old_revision_lost':
        first_key = next(iter(baseline['current'])); fresh['histories'][first_key].pop(0)
    elif attack == 'wrong_first_revision':
        first = list(history[0]); first[1] = 2; history[0] = tuple(first)
    else:
        refs[fresh['events'][len(baseline['events'])][0]]['namespace'] = namespace
    with pytest.raises(ValueError):
        audit.check_saved_batches(fresh, baseline, tasks, registry, refs)


@pytest.fixture
def fresh_catalog(catalog_case):
    case = catalog_case
    for n in range(1494, 1503):
        key = f'{n+1:024x}'; source = row(key)
        proposal = dict(target_key=key, polygon=copy.deepcopy(POLYGON))
        value = event(key); value.update(proposal_publication=15, base_proposal_id=audit.sha(proposal), proposal_polygon=copy.deepcopy(POLYGON))
        raw = json.dumps(value, ensure_ascii=False, indent=2); event_id = n-481
        case['rows'][key] = source; case['gallery']['current'][key] = (1, raw, value)
        case['gallery']['histories'][key] = [(event_id, 1, raw, value)]; case['gallery']['events'].append((event_id, key, 1, raw))
        case['registry'][15, audit.sha(proposal), key] = dict(kind='independent_preannotation', namespace='pallet_mask_under50_requests_v2',
            proposal=proposal, path=Path('synthetic-v2'))
        case['catalog'].append(dict(target_key=key, row=source, polygon=copy.deepcopy(POLYGON), origin='saved_gallery', revision=1,
            payload_sha256=audit.sha(value), event_raw_sha256=audit.sources.raw_sha(raw), initial_polygon=copy.deepcopy(POLYGON),
            batch='pallet_mask_under50_requests_v2', history_revisions=1, actual_saved_research_reference_not_GT=True))
        case['event_audit'].append(dict(event_id=event_id, target_key=key, revision=1, payload_raw_sha256=audit.sources.raw_sha(raw),
            proposal_publication=15, base_proposal_id=value['base_proposal_id'], namespace='pallet_mask_under50_requests_v2', event_specific_source_verified=True))
    write(case['snapshot']/'private/source_catalog.jsonl', case['catalog'], True)
    write(case['snapshot']/'private/event_binding_audit.jsonl', case['event_audit'], True)
    return case


def check_catalog(case):
    return audit.check_fresh_catalog(case['snapshot'], case['baseline'], case['pose'], case['gallery'], case['rows'],
        case['publications'], case['registry'], 'f'*64, None)


def test_all1503_catalog_sources_and1021_current_gallery_event_bindings_are_recomputed(fresh_catalog):
    catalog, refs = check_catalog(fresh_catalog)
    assert len(catalog) == 1503 and len(refs) == len(fresh_catalog['checked']) == 1021


@pytest.mark.parametrize('attack', ['stale_polygon', 'canonicalized_raw_hash', 'wrong_namespace', 'duplicate_frame', 'original_rectangle'])
def test_fresh_catalog_cannot_keep_stale_labels_or_fabricate_latest_raw_hashes(fresh_catalog, attack):
    case = fresh_catalog; record = case['catalog'][-1]
    if attack == 'stale_polygon':
        record['polygon'][0][0] += 1
    elif attack == 'canonicalized_raw_hash':
        record['event_raw_sha256'] = audit.sha(case['gallery']['current'][record['target_key']][2])
    elif attack == 'wrong_namespace':
        record['batch'] = 'pallet_mask_under50_requests_v1'
    elif attack == 'duplicate_frame':
        record['row']['frame_id'] = case['catalog'][-2]['row']['frame_id']
    else:
        value = case['gallery']['current'][record['target_key']][2]
        value['polygon'] = [[20, 30], [80, 30], [80, 50], [20, 50]]
    write(case['snapshot']/'private/source_catalog.jsonl', case['catalog'], True)
    with pytest.raises(ValueError):
        check_catalog(case)


def count_case():
    catalog, inputs, manifest = refreshed_count_fixture()
    for cls, count in {11: 2, 16: 2, 17: 2, 19: 1, 20: 2}.items():
        for n in range(count):
            key = f'{cls+300:022x}{n:02x}'; catalog.append(dict(target_key=key, row=row(key, cls)))
    counts = audit.Counter(r['row']['final_class_id'] for r in catalog)
    inputs.update(actual_counted_saved_basis=1503, actual_class_counts={str(c): n for c, n in counts.items()})
    manifest['actual_human_saved_counts_before'] = copy.deepcopy(inputs['actual_class_counts'])
    return catalog, inputs, manifest


def test_current_real_counts_are1503_class20has49_and_old_verifiers_keep_old_counts():
    counts = audit.check_count_basis(*count_case())
    assert counts[20] == 49 and sum(counts.values()) == 1503
    assert audit.continuation.EXPECTED_COUNTS[20] == 47 and audit.geometry.EXPECTED_COUNTS[20] == 45


@pytest.mark.parametrize('attack', ['stale1494', 'machine1550', 'wrong_class20', 'duplicate_new_frame', 'missing_class'])
def test_false_or_stale_manual_counts_are_not_accepted(attack):
    catalog, inputs, manifest = count_case()
    if attack == 'stale1494':
        inputs['actual_counted_saved_basis'] = 1494
    elif attack == 'machine1550':
        inputs['actual_counted_saved_basis'] = 1550
    elif attack == 'wrong_class20':
        inputs['actual_class_counts']['20'] = 47
    elif attack == 'duplicate_new_frame':
        catalog[-1]['row']['frame_id'] = catalog[-2]['row']['frame_id']
    else:
        del inputs['actual_class_counts']['1']
    with pytest.raises(ValueError):
        audit.check_count_basis(catalog, inputs, manifest)


def test_class20_may_stage_and_issue_only1_not2_or3():
    counts = audit.EXPECTED_COUNTS
    audit.check_staged_limits([dict(class_id=20)], counts)
    for n in (2, 3):
        with pytest.raises(ValueError, match='class20 permits only1'):
            audit.check_staged_limits([dict(class_id=20)]*n, counts)
    staged = [dict(target_key=f'{n:024x}', class_id=20) for n in range(2)]
    decisions = {r['target_key']: dict(approved=True) for r in staged}
    manifest = dict(request_count=2, classes=[20], class_counts={'20': 2})
    with pytest.raises(ValueError, match='class maximum/gap'):
        audit.geometry.check_issued_inventory(staged, copy.deepcopy(staged), staged, decisions, manifest, counts)


def test_all36_previous_staged_images_including_rejects_are_global_exclusions(tmp_path):
    own = tmp_path/'outputs/pallet_mask_under50_requests_v3'; rows = {}
    for version in (1, 2):
        records = []
        for n in range(18):
            key = f'{version:022x}{n:02x}'; rows[key] = row(key); records.append(dict(target_key=key, approved=False))
        root = tmp_path/f'outputs/pallet_mask_under50_requests_v{version}'
        write(root/'internal/shortlist.jsonl', records, True); write(root/'requests.jsonl', records[:10 if version == 1 else 9], True)
    write(own/'internal/shortlist.jsonl', [dict(target_key='f'*24)], True)
    hashes, keys = audit.geometry.prior_inspections(tmp_path, own, rows)
    assert len(keys) == 36 and len(hashes) == 4


def test_refreshed_public_counts_and_filters_are_explicit_without_mutating_old_validator(public_pack, monkeypatch):
    root, manifest, proposals, _ = public_pack; monkeypatch.setattr(audit, 'PROJECT', audit.geometry.PROJECT)
    with pytest.raises(ValueError, match='real-human counts'):
        audit.check_public(root, manifest, proposals)
    page = (root/'report_zh.html').read_text()
    for c in audit.CLASSES:
        page = page.replace(f'Class {c}：{audit.geometry.EXPECTED_COUNTS[c]}份', f'Class {c}：{audit.EXPECTED_COUNTS[c]}份')
    (root/'report_zh.html').write_text(page); manifest['public_assets']['report_zh.html'] = audit.digest(root/'report_zh.html')
    assert audit.check_public(root, manifest, proposals) == set(manifest['public_assets'])


def test_new_input_and_manifest_must_explicitly_bind_fresh_audit_not_old_quality_seal(tmp_path, monkeypatch):
    snapshot = tmp_path/'snapshot'; write(snapshot/'audit.json', dict(source='synthetic v4'))
    digest = audit.digest(snapshot/'audit.json'); monkeypatch.setattr(audit, 'SOURCE_AUDIT_SHA256', digest)
    audit.check_source_binding(dict(source_audit_sha256=digest), dict(source_audit_sha256=digest), snapshot)
    with pytest.raises(ValueError):
        audit.check_source_binding(dict(source_audit_sha256=digest), dict(source_audit_sha256=audit.geometry.QUALITY_SEAL_SHA256), snapshot)
    with pytest.raises(KeyError):
        audit.check_source_binding(dict(source_audit_sha256=digest), {}, snapshot)


def source_claims_case():
    catalog = [dict(row=dict(final_class_id=11)) for _ in range(1503)]
    pose = dict(fingerprint={'schema_sha256': 'pose', 'tables': {'annotations': 'raw'}})
    gallery_store = dict(fingerprint={'schema_sha256': 'gallery', 'tables': {'events': 'raw'}})
    extension = dict(changed_baseline_current_keys=['old-key'], new_distinct_saved_keys=[f'{n:024x}' for n in range(19)])
    manifests = dict(v1='a', v2='b', v3='c', pallet_mask_under50_requests_v1='d', pallet_mask_under50_requests_v2='e')
    trained = {'old-key': 'train'}
    summary = dict(state='passed_event_provenance_and_distinct_saved_count_not_contour_accuracy', total_contours=1503,
        gallery_contours=1021, gallery_events=1126, pose_contours=482, old_raw_event_prefix_preserved=1088,
        production_annotation_writes=0, pose_fingerprint=copy.deepcopy(pose['fingerprint']),
        gallery_fingerprint=copy.deepcopy(gallery_store['fingerprint']), class_counts={'11': 1503},
        changed_baseline_current_keys=['old-key'], new_distinct_saved_keys=copy.deepcopy(extension['new_distinct_saved_keys']),
        actual_changed_baseline_revisions_not_new_distinct_images=True, baseline_current_revisions_preserved_in_history=True,
        frozen_training_dataset_modified=False, source_registry_task_manifests=copy.deepcopy(manifests),
        trained_reference_keys_with_newer_human_revision=copy.deepcopy(trained))
    return summary, catalog, pose, gallery_store, extension, manifests, trained


def test_producer_source_claims_are_recomputed_against_all_current_references():
    audit.check_source_claims(*source_claims_case())


@pytest.mark.parametrize('attack', ['old1494', 'old1115', 'new_images_not19', 'duplicate_changed_key', 'frozen_training_rewritten',
    'wrong_registry', 'old_fingerprint', 'accuracy_claim', 'boolean_writes', 'wrong_training_split'])
def test_false_producer_denominators_triples_or_training_claims_are_not_independent_truth(attack):
    args = source_claims_case(); summary = args[0]
    if attack == 'old1494':
        summary['total_contours'] = 1494
    elif attack == 'old1115':
        summary['gallery_events'] = 1115
    elif attack == 'new_images_not19':
        summary['new_distinct_saved_keys'].pop()
    elif attack == 'duplicate_changed_key':
        summary['changed_baseline_current_keys'] *= 2
    elif attack == 'frozen_training_rewritten':
        summary['frozen_training_dataset_modified'] = True
    elif attack == 'wrong_registry':
        del summary['source_registry_task_manifests']['pallet_mask_under50_requests_v2']
    elif attack == 'old_fingerprint':
        summary['gallery_fingerprint']['tables']['events'] = 'old1115'
    elif attack == 'accuracy_claim':
        summary['state'] = 'passed_contour_accuracy'
    elif attack == 'boolean_writes':
        summary['production_annotation_writes'] = False
    else:
        summary['trained_reference_keys_with_newer_human_revision']['old-key'] = 'validation'
    with pytest.raises(ValueError):
        audit.check_source_claims(*args)


def test_geometry_export_helpers_do_not_promote_real_four_point_draft_to_human_or_old_seed(staged):
    proposal, task = draft(staged); image_sha = audit.digest(staged['formal']/staged['row']['image'])
    audit.geometry.check_draft(proposal, task, staged['record'], staged['decision'], 'e'*64, 'f'*64, image_sha)
    audit.geometry.check_exports(staged['root'], export(staged), staged['record'], image_sha)
    assert len(proposal['polygon']) == 4 and not proposal['propagation_seed_eligible'] and not task['counts_as_completed_annotation']


@pytest.fixture
def seal_root(tmp_path, monkeypatch):
    monkeypatch.setattr(audit, 'PROJECT', tmp_path)
    root = tmp_path/'outputs/pallet_mask_under50_requests_v3'; root.mkdir(parents=True); write(root/'manifest.json', {})
    return root


def test_no_completion_notification_means_no_staging_audit_or_seals(seal_root):
    def forbidden(_):
        pytest.fail('unfinished staging audited')
    with pytest.raises(ValueError, match='completion'):
        audit.seal(seal_root, auditor=forbidden)
    assert set(audit.inventory(seal_root)) == {'manifest.json'}


def test_only_successful_completed_audit_creates_two_identical_private_exclusive_seals(seal_root):
    result = dict(state='passed', request_count=9, semantic_contour_accuracy_established=False)
    audit.seal(seal_root, producer_complete=True, auditor=lambda _: result)
    assert (seal_root/audit.SEALS[0]).read_bytes() == (seal_root/audit.SEALS[1]).read_bytes() == audit.canonical(result)+b'\n'
    assert all((seal_root/name).stat().st_mode & 0o777 == 0o600 for name in audit.SEALS)
    with pytest.raises(ValueError, match='Never overwrite'):
        audit.seal(seal_root, producer_complete=True, auditor=lambda _: result)


@pytest.mark.parametrize('state', ['failed', 'pending', 'passed_event_provenance'])
def test_nonpassing_task_result_never_creates_passed_seals(seal_root, state):
    with pytest.raises(ValueError, match='did not pass'):
        audit.seal(seal_root, producer_complete=True, auditor=lambda _: dict(state=state))
    assert set(audit.inventory(seal_root)) == {'manifest.json'}


def test_race_on_second_seal_preserves_other_writer_and_rolls_back_only_own_first_file(seal_root):
    def raced(root):
        (root/'independent_verification.json').write_bytes(b'other writer')
        return dict(state='passed')
    with pytest.raises(FileExistsError):
        audit.seal(seal_root, producer_complete=True, auditor=raced)
    assert not (seal_root/'verification.json').exists()
    assert (seal_root/'independent_verification.json').read_bytes() == b'other writer'


def test_prior_output_and_symlink_cannot_receive_new_v3_seals(seal_root):
    old = seal_root.parent/'pallet_mask_under50_requests_v2'; old.mkdir(); write(old/'manifest.json', {})
    for root in (old, seal_root.parent/'alias'):
        if root.name == 'alias':
            root.symlink_to(seal_root, target_is_directory=True)
        with pytest.raises(ValueError):
            audit.seal(root, producer_complete=True, auditor=lambda _: dict(state='passed'))
