"""Independent extension audit tests; never invoke either generator or live stores."""
import copy
import hashlib
import json
import sqlite3

import pytest

from scripts import verify_mask_quota_saved_extension as audit


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=key+'::box:0000', frame_id='frame-'+key,
        final_class_id=cls, split='train', policy_id='camera', viewpoint_candidate='front',
        group_id='group', box_index=0, bbox=dict(x=1, y=1, width=19, height=9),
        size=[24, 16], source_pcd_sha256='pcd')


@pytest.fixture
def saved_fixture():
    key = sorted(audit.EXTRA_KEYS)[0]
    r = row(key)
    current = dict(target_key=key, source=r, revision=7, state='draft', polygon=copy.deepcopy(POLYGON),
        human_mask_confirmed=False, human_pose_confirmed=False, automatic_training_eligible=False, robot_command_eligible=False)
    raw = json.dumps(current, indent=2)
    pose = dict(current={key: current}, events=[(13, key, 7, raw)])
    direct = audit.v1.independent_pose_templates(pose, {key: r})
    t = direct[key]
    binding = dict(target_key=key, class_id=10, split='train', snapshot_revision=7,
        current_payload_sha256=audit.sha(current), human_polygon_revision=7,
        human_event_sha256=hashlib.sha256(raw.encode()).hexdigest(), vertex_count=8,
        template_human_confirmed=False, origin=audit.v1.POSE_ORIGIN, pose_review=audit.v1.pose_provenance(t, 'snapshot'))
    proposal = dict(target_key=key, class_id=10, source=r, polygon=copy.deepcopy(POLYGON), vertex_count=8,
        schema='saved_pose_mask_gallery_entry_v1', origin=audit.v1.POSE_ORIGIN,
        status='human_edited_draft_pending_confirmation', human_edited=True, pose_snapshot_saved=True,
        source_label_kind='human_mask_source_manual_template_draft',
        pose_store_written=False, gallery_store_written=False, **dict.fromkeys(audit.v1.FALSE_FLAGS, False),
        metrics={}, snapshot_sha256='snapshot', target_expected_revision=7,
        target_snapshot_payload_sha256=audit.sha(current), formal_manifest_sha256='formal',
        class_assignments_sha256='classes', template=binding, rendering_id='quota_generation:'+key,
        manual_source_kind='authorized_existing_human_mask_reuse', machine_transfer_performed=False, new_human_edit_created=False,
        flags=['manual_source_not_machine_transfer'])
    return (proposal, direct, pose, {key: r}, 'snapshot', 'formal', 'classes')


def test_existing_saved_source_is_checked_without_faking_a_request(saved_fixture):
    audit.check_saved_human(*saved_fixture, extra=True)
    p = saved_fixture[0]
    assert 'manual_request_sha256' not in p
    assert p['template']['target_key'] == p['target_key']


@pytest.mark.parametrize('attack', ['request', 'polygon', 'count', 'event_hash', 'payload_hash', 'snapshot',
    'revision', 'confirmed', 'training', 'robot', 'GT', 'machine_metrics', 'continuation', 'self_source',
    'class', 'split', 'deletion', 'deferred', 'source_gt', 'gallery_write', 'not_human'])
def test_saved_extension_rejects_fabricated_stale_or_confirmed_labels(saved_fixture, attack):
    p, _, pose, _, *_ = saved_fixture
    if attack == 'request':
        p['manual_request_sha256'] = 'fake-original-request'
    elif attack == 'polygon':
        p['polygon'][0][0] += 1
    elif attack == 'count':
        p['vertex_count'] += 1
    elif attack == 'event_hash':
        p['template']['human_event_sha256'] = audit.sha(pose['current'][p['target_key']])
    elif attack == 'payload_hash':
        p['target_snapshot_payload_sha256'] = 'stale'
    elif attack == 'snapshot':
        p['snapshot_sha256'] = 'stale'
    elif attack == 'revision':
        p['target_expected_revision'] -= 1
    elif attack in ('confirmed', 'training', 'robot', 'GT'):
        p[dict(confirmed='human_mask_confirmed', training='automatic_training_eligible', robot='robot_command_eligible', GT='ground_truth_eligible')[attack]] = True
    elif attack == 'machine_metrics':
        p['metrics'] = {'ecc': 1.0}
    elif attack == 'continuation':
        p['continuation'] = {'batch': 6}
    elif attack == 'self_source':
        p['template']['target_key'] = 'another-label'
    elif attack == 'class':
        p['class_id'] = 2
    elif attack == 'split':
        p['source'] = dict(p['source'], split='test')
    elif attack in ('deletion', 'deferred'):
        pose['current'][p['target_key']]['state'] = 'deleted' if attack == 'deletion' else 'deferred'
    elif attack == 'source_gt':
        pose['current'][p['target_key']]['ground_truth_eligible'] = True
    elif attack == 'gallery_write':
        p['gallery_store_written'] = True
    else:
        p['human_edited'] = False
    with pytest.raises(ValueError):
        audit.check_saved_human(*saved_fixture, extra=True)


def partition_fixture():
    old = {'old-'+str(i): dict(target_key='old-'+str(i)) for i in range(140)}
    original = {'manual-'+str(i): dict(target_key='manual-'+str(i)) for i in range(24)}
    machines = {'machine-'+str(i): dict(target_key='machine-'+str(i)) for i in range(67)}
    base = {**old, **original, **machines}
    extra = {k: dict(target_key=k) for k in audit.EXTRA_KEYS}
    manual, combined = {**original, **extra}, {**base, **extra}
    return base, old, original, manual, extra, machines, combined, machines


def test_231_canonical_records_are_preserved_with_only_six_human_additions():
    args = partition_fixture()
    audit.check_partition(*args)
    args[-2]['old-0'] = dict(target_key='old-0', changed=True)
    with pytest.raises(ValueError, match='Original v1 canonical object changed'):
        audit.check_partition(*args)


def test_six_manual_sources_cannot_leak_into_machine_adoption_sidecar():
    args = list(partition_fixture())
    args[-1] = {**args[-1], **args[4]}
    with pytest.raises(ValueError, match='accepted transfer'):
        audit.check_partition(*args)


def test_unapproved_seventh_saved_source_cannot_be_implicitly_counted():
    args = partition_fixture()
    args[4]['unapproved'] = {'target_key': 'unapproved'}
    with pytest.raises(ValueError, match='exactly the six'):
        audit.check_partition(*args)


def test_only_the_six_protected_human_outcome_reasons_may_change():
    base = {k: dict(target_key=k, class_id=10, reason='human_mask_history_protected') for k in audit.EXTRA_KEYS}
    base['machine'] = dict(target_key='machine', class_id=2, reason='new_proposal')
    outcomes = {k: dict(v, reason='saved_human_source_added') if k in audit.EXTRA_KEYS else v for k, v in base.items()}
    audit.check_outcomes(base, outcomes, audit.EXTRA_KEYS)
    outcomes['machine'] = dict(outcomes['machine'], reason='saved_human_source_added')
    with pytest.raises(ValueError, match='unrelated outcome'):
        audit.check_outcomes(base, outcomes, audit.EXTRA_KEYS)


def test_extra_label_must_already_have_protected_human_history():
    key = sorted(audit.EXTRA_KEYS)[0]
    base = {key: dict(target_key=key, class_id=10, reason='new_proposal')}
    outcomes = {key: dict(base[key], reason='saved_human_source_added')}
    with pytest.raises(ValueError, match='already protected human history'):
        audit.check_outcomes(base, outcomes, {key})


def write_json(path, value):
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(audit.canonical(value)+b'\n')


@pytest.fixture
def sealed_base(tmp_path, monkeypatch):
    root, project = tmp_path/'base', tmp_path/'project'
    root.mkdir()
    code_pins = {}
    for name in ('scripts/verify_mask_quota_completion.py', 'pallet/mask_quota_completion.py'):
        path = project/name
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text('frozen independent audit / generator')
        code_pins[name] = audit.digest(path)
    manifest = dict(publication_version=6, proposals=231, manual_proposals=24, new_proposals=67, previous_proposals=140,
        code_sha256=code_pins['pallet/mask_quota_completion.py'], method='original', accepted_before_diversity=144,
        generation_templates=175)
    (root/'proposals.jsonl').write_text('{}\n'*231)
    (root/'registration_attempts.jsonl').write_text('{}\n'*13347)
    manifest['proposals_sha256'] = audit.digest(root/'proposals.jsonl')
    write_json(root/'manifest.json', manifest)
    artifacts = {n: audit.digest(root/n) for n in ('manifest.json', 'proposals.jsonl', 'registration_attempts.jsonl')}
    write_json(root/'artifact_hashes.json', artifacts)
    base_pins = {n: audit.digest(root/n) for n in ('manifest.json', 'proposals.jsonl', 'artifact_hashes.json')}
    seal = dict(state='passed', publication_version=6, proposals_verified=231, manual_verified=24, new_verified=67,
        old_canonical_objects_preserved=140, proposals_sha256=manifest['proposals_sha256'], manifest_sha256=base_pins['manifest.json'],
        artifact_hashes_sha256=base_pins['artifact_hashes.json'],
        verification_script_sha256=code_pins['scripts/verify_mask_quota_completion.py'], method='original',
        thresholds_relaxed=False, forced_fill=False, human_confirmation_inferred=False, production_database_writes=0,
        registration_attempts_verified=13347, accepted_before_diversity_recomputed=144, templates_verified=175)
    for field in ('all_new_registrations_and_metrics_recomputed', 'all_rejected_attempts_and_metrics_recomputed',
                  'manual_exact_sqlite_payloads_and_human_events_verified', 'manual_sources_not_machine_transfers',
                  'live_dependencies_unchanged', 'authorization_dependencies_unchanged'):
        seal[field] = True
    write_json(root/'validation_report.json', seal)
    seal['validation_report_sha256'] = audit.digest(root/'validation_report.json')
    write_json(root/'verification.json', seal)
    monkeypatch.setattr(audit, 'BASE', root)
    monkeypatch.setattr(audit, 'PROJECT', project)
    monkeypatch.setattr(audit, 'BASE_PINS', base_pins)
    monkeypatch.setattr(audit, 'CODE_PINS', code_pins)
    monkeypatch.setattr(audit.v1, 'check_frozen_inputs', lambda: {})
    return root, seal


def test_complete_original_attempt_audit_can_be_inherited_by_seal(sealed_base):
    result = audit.check_base()
    assert result['seal']['registration_attempts_verified'] == 13347
    assert result['seal_sha256'] == audit.digest(sealed_base[0]/'verification.json')


@pytest.mark.parametrize('attack', ['not_passed', 'wrong_proposals', 'wrong_manifest', 'wrong_artifacts',
    'wrong_verifier', 'no_failed_replay', 'multiscale', 'altered_attempts', 'altered_code', 'missing_seal'])
def test_inheritance_rejects_unsealed_forged_changed_or_partial_base(sealed_base, attack):
    root, seal = sealed_base
    if attack == 'altered_attempts':
        (root/'registration_attempts.jsonl').write_text('changed-machine-evidence')
    elif attack == 'altered_code':
        (audit.PROJECT/'pallet/mask_quota_completion.py').write_text('changed original pipeline')
    elif attack == 'missing_seal':
        (root/'verification.json').unlink()
    else:
        key, value = {'not_passed': ('state', 'failed'), 'wrong_proposals': ('proposals_sha256', 'fake'),
            'wrong_manifest': ('manifest_sha256', 'fake'), 'wrong_artifacts': ('artifact_hashes_sha256', 'fake'),
            'wrong_verifier': ('verification_script_sha256', 'fake'),
            'no_failed_replay': ('all_rejected_attempts_and_metrics_recomputed', False), 'multiscale': ('method', 'multiscale')}[attack]
        seal[key] = value
        # Keep the report consistent so this tests substantive seal requirements too.
        report = {k: v for k, v in seal.items() if k != 'validation_report_sha256'}
        write_json(root/'validation_report.json', report)
        seal['validation_report_sha256'] = audit.digest(root/'validation_report.json')
        write_json(root/'verification.json', seal)
    with pytest.raises(ValueError):
        audit.check_base()


def test_full_source_archive_preserves_v1_seal_and_superseded_metadata(sealed_base, tmp_path):
    base = audit.check_base()
    out = tmp_path/'extension'
    source_files = dict(base['artifacts'], **{'artifact_hashes.json': audit.BASE_PINS['artifact_hashes.json'],
        'validation_report.json': base['seal']['validation_report_sha256'], 'verification.json': base['seal_sha256']})
    preserved = {}
    for name, expected in source_files.items():
        destination = out/('source_generation_evidence/'+name if name in audit.ARCHIVED_METADATA else name)
        destination.parent.mkdir(parents=True, exist_ok=True)
        destination.write_bytes((audit.BASE/name).read_bytes())
        if name not in audit.ARCHIVED_METADATA:
            preserved[name] = expected
    write_json(out/'source_generation_inventory.json', source_files)
    write_json(out/'preserved_source_generation_assets.json', preserved)
    assert audit.check_source_archive(out, base) == source_files
    (out/'source_generation_evidence/verification.json').write_text('fake copied success seal')
    with pytest.raises(ValueError, match='source/archive changed'):
        audit.check_source_archive(out, base)


def test_every_inherited_machine_attempt_and_native_asset_is_byte_identical(tmp_path, monkeypatch):
    base, out = tmp_path/'base', tmp_path/'extension'
    names = {'new_proposals.jsonl', 'accepted_candidates.jsonl', 'registration_attempts.jsonl', 'requests.jsonl',
        'reviews_snapshot.sqlite3', 'gallery_reviews_snapshot.sqlite3', 'refined_annotations.jsonl',
        'protected_formal_before.json', 'protected_formal_after.json', 'preserved_parent_assets.json',
        'vertex2_before_after.png', 'FONT_LICENSE.txt', 'images/old.png', 'cases/old.png',
        'masks/old.png', 'labelme/old.json', 'quota_generation/templates/seed.json', 'backups/reviews_snapshot.sqlite3'}
    artifacts = {}
    for name in names:
        for root in (base, out):
            path = root/name
            path.parent.mkdir(parents=True, exist_ok=True)
            path.write_bytes(('frozen '+name).encode())
        artifacts[name] = audit.digest(base/name)
    monkeypatch.setattr(audit, 'BASE', base)
    assert audit.check_inherited_files(out, artifacts) == names
    (out/'registration_attempts.jsonl').write_text('new unverified transfer attempts')
    with pytest.raises(ValueError, match='Inherited v1 asset/evidence changed'):
        audit.check_inherited_files(out, artifacts)


def test_template_pool_cannot_be_extended_or_historical_mappings_changed():
    args = partition_fixture()
    _, old, original, manual, extra, machines, combined, _ = args
    base_mapping = {k: 'historical:seed' for k in set(combined)-set(extra)}
    latest = [dict(row=row(k), origin=audit.v1.POSE_ORIGIN, rendering_id='quota_generation:'+k) for k in extra]
    base_inventory = dict(templates=latest, rendering_by_target=base_mapping,
        generation_template_namespace='quota_generation', generation_template_keys=sorted(extra),
        historical_templates=99, generation_templates_by_origin={audit.v1.POSE_ORIGIN: 6},
        generation_templates=175, eligibility={}, snapshot_saved=4758, snapshot_events=10290,
        gallery_snapshot_saved=97, previous_proposals=140)
    inventory = copy.deepcopy(base_inventory)
    inventory.update(manual_proposals=30, counted_manual_source_keys=sorted(manual), manual_supplement_keys=sorted(extra))
    inventory['rendering_by_target'].update({k: 'quota_generation:'+k for k in extra})
    audit.check_rendering(base_inventory, inventory, old, manual, machines, extra)
    inventory['templates'].append(dict(row=row('unverified-new-seed'), rendering_id='quota_generation:new'))
    with pytest.raises(ValueError, match='frozen template/eligibility inventory'):
        audit.check_rendering(base_inventory, inventory, old, manual, machines, extra)


def test_failed_extension_audit_never_writes_a_success_seal(tmp_path, monkeypatch):
    out = tmp_path/'extension'
    out.mkdir()
    (out/'manifest.json').write_text('{}')
    monkeypatch.setattr(audit, 'OUTPUT', out)
    def fail(*_):
        raise ValueError('stale existing human event')
    monkeypatch.setattr(audit, 'audit', fail)
    with pytest.raises(ValueError, match='stale existing human event'):
        audit.main(['--output', str(out)])
    assert not (out/'verification.json').exists()
    assert json.loads((out/'validation_report.json').read_text())['state'] == 'failed'


def test_extension_seal_has_gallery_contract_and_cannot_be_overwritten(tmp_path, monkeypatch):
    out = tmp_path/'extension'
    out.mkdir()
    (out/'manifest.json').write_text('{}')
    monkeypatch.setattr(audit, 'OUTPUT', out)
    result = dict(state='passed', publication_version=6, proposals_verified=237, proposals_sha256='sealed',
        old_canonical_objects_preserved=140, manual_verified=30, new_verified=67, method='original',
        extra_public_images=['vertex2_before_after.png'])
    monkeypatch.setattr(audit, 'audit', lambda *_: dict(result))
    audit.main(['--output', str(out)])
    sealed = json.loads((out/'verification.json').read_text())
    assert sealed['proposals_verified'] == 237 and sealed['publication_version'] == 6
    assert sealed['validation_report_sha256'] == audit.digest(out/'validation_report.json')
    before = (out/'verification.json').read_bytes()
    with pytest.raises(ValueError, match='overwrite a frozen verification'):
        audit.main(['--output', str(out)])
    assert (out/'verification.json').read_bytes() == before


def test_extension_symlink_cannot_overwrite_any_frozen_source_report(tmp_path, monkeypatch):
    source = tmp_path/'frozen'
    source.mkdir()
    (source/'manifest.json').write_text('{}')
    (source/'validation_report.json').write_text('keep original')
    link = tmp_path/'extension'
    link.symlink_to(source, target_is_directory=True)
    monkeypatch.setattr(audit, 'OUTPUT', link)
    with pytest.raises(ValueError, match='v2 outputs directory only'):
        audit.main(['--output', str(link)])
    assert (source/'validation_report.json').read_text() == 'keep original'


def test_live_manual_current_and_human_event_require_exact_raw_bytes(tmp_path):
    path = tmp_path/'live.sqlite3'
    raw = '{"polygon": [[1, 1]], "revision": 7}'
    records = {'human': dict(current_revision=7, current_payload_raw=raw,
        human_event_id=13, human_polygon_revision=7, human_event_payload_raw=raw)}
    with sqlite3.connect(path) as db:
        db.executescript('CREATE TABLE annotations(target_key TEXT, revision INTEGER, payload TEXT);'
                         'CREATE TABLE events(id INTEGER, target_key TEXT, revision INTEGER, payload TEXT);')
        db.execute('INSERT INTO annotations VALUES (?,?,?)', ('human', 7, raw))
        db.execute('INSERT INTO events VALUES (?,?,?,?)', (13, 'human', 7, raw))
    before = path.read_bytes()
    audit.check_live_manual_records(path, records)
    assert path.read_bytes() == before
    with sqlite3.connect(path) as db:
        db.execute('UPDATE annotations SET payload=?', (raw+' ',))
    with pytest.raises(ValueError, match='raw SQLite bytes changed'):
        audit.check_live_manual_records(path, records)
