#!/usr/bin/env python3
"""Audit issue 61's native inventory model, store, and cache boundary."""

from __future__ import annotations

from pathlib import Path
import re

import generate_api_shims


ROOT = Path(__file__).resolve().parents[1]
SOURCE = ROOT / "crates" / "libremetaverse" / "src" / "inventory.rs"
DOC = ROOT / "docs" / "inventory.md"
STORE_TESTS = ROOT / "tests" / "compat" / "tests" / "inventory_store_semantics.rs"
LINK_TESTS = ROOT / "tests" / "compat" / "tests" / "misclassified_link_semantics.rs"
STUB_RE = re.compile(r"\b(?:not_implemented|unimplemented_api)\b|\b(?:todo|unimplemented)!\s*\(")

REQUIRED_TYPES = {
    "T:LibreMetaverse.Inventory",
    "T:LibreMetaverse.InventoryAnimation",
    "T:LibreMetaverse.InventoryAttachment",
    "T:LibreMetaverse.InventoryBase",
    "T:LibreMetaverse.InventoryCallingCard",
    "T:LibreMetaverse.InventoryCategory",
    "T:LibreMetaverse.InventoryFolder",
    "T:LibreMetaverse.InventoryGesture",
    "T:LibreMetaverse.InventoryItem",
    "T:LibreMetaverse.InventoryLSL",
    "T:LibreMetaverse.InventoryLandmark",
    "T:LibreMetaverse.InventoryMaterial",
    "T:LibreMetaverse.InventoryNode",
    "T:LibreMetaverse.InventoryNodeDictionary",
    "T:LibreMetaverse.InventoryNotecard",
    "T:LibreMetaverse.InventoryObject",
    "T:LibreMetaverse.InventoryObjectAddedEventArgs",
    "T:LibreMetaverse.InventoryObjectRemovedEventArgs",
    "T:LibreMetaverse.InventoryObjectUpdatedEventArgs",
    "T:LibreMetaverse.InventorySettings",
    "T:LibreMetaverse.InventorySnapshot",
    "T:LibreMetaverse.InventorySound",
    "T:LibreMetaverse.InventoryTexture",
    "T:LibreMetaverse.InventoryWearable",
    "T:LibreMetaverse.Permissions",
}


def require_markers(path: Path, markers: tuple[str, ...]) -> None:
    text = path.read_text()
    missing = [marker for marker in markers if marker not in text]
    if missing:
        raise SystemExit(f"{path.name}: audit evidence missing: " + ", ".join(missing))


def main() -> None:
    missing = sorted(REQUIRED_TYPES - generate_api_shims.NATIVE_TYPES.keys())
    if missing:
        raise SystemExit("issue 61 native types missing: " + ", ".join(missing))
    if STUB_RE.search(SOURCE.read_text()):
        raise SystemExit("issue 61 owned Rust stubs remain in inventory.rs")
    require_markers(SOURCE, (
        "MAX_CACHE_BYTES", "MAX_CACHE_RECORDS", "MAX_HIERARCHY_DEPTH",
        "rebuild_indexes_and_counts", "replace_file", "would_create_cycle",
        "callbacks_run_after_store_lock_is_released",
        "cache_rejects_corruption_and_unknown_versions",
        "cache_round_trip_preserves_roots_concrete_type_and_permissions",
        "system_folders_sort_first_and_are_discoverable",
    ))
    require_markers(DOC, ("misclassified-link", "64 MiB", "rollback", "No inventory"))
    require_markers(STORE_TESTS, ("concurrent_add_move_remove_stress", "link_index_updates_on_asset_change"))
    require_markers(LINK_TESTS, ("inventory_store_indexer_returns_wearable", "resolved_item_id_on_misclassified_texture_link"))
    print("issue 61 audit: native models, concrete hierarchy storage, bounded cache, lock-free callbacks, and compatibility regressions are present")


if __name__ == "__main__":
    main()
