#!/usr/bin/env python3
"""Audit issue 60's native movement, teleport, and crossing 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" / "agent_movement.rs"
DOC = ROOT / "docs" / "agent-movement.md"
STUB_RE = re.compile(r"\b(?:not_implemented|unimplemented_api)\b|\b(?:todo|unimplemented)!\s*\(")

REQUIRED_TYPES = {
    "T:LibreMetaverse.AgentManager.AgentMovement",
    "T:LibreMetaverse.AgentManager.AgentMovement.AgentCamera",
    "T:LibreMetaverse.AvatarSitResponseEventArgs",
    "T:LibreMetaverse.CameraConstraintEventArgs",
    "T:LibreMetaverse.RegionCrossedEventArgs",
    "T:LibreMetaverse.RegionCrossingPredictionEventArgs",
    "T:LibreMetaverse.TeleportEventArgs",
}

REQUIRED_MEMBERS = {
    "E:LibreMetaverse.AgentManager.AvatarSitResponse",
    "E:LibreMetaverse.AgentManager.CameraConstraint",
    "E:LibreMetaverse.AgentManager.RegionCrossed",
    "E:LibreMetaverse.AgentManager.RegionCrossingPredicted",
    "E:LibreMetaverse.AgentManager.TeleportProgress",
    "M:LibreMetaverse.AgentManager.AutoPilot(System.Double,System.Double,System.Double)",
    "M:LibreMetaverse.AgentManager.CancelCrossing",
    "M:LibreMetaverse.AgentManager.CompleteAgentMovement(LibreMetaverse.Simulator)",
    "M:LibreMetaverse.AgentManager.Crouch(System.Boolean)",
    "M:LibreMetaverse.AgentManager.Fly(System.Boolean)",
    "M:LibreMetaverse.AgentManager.GetCrossingDetails",
    "M:LibreMetaverse.AgentManager.GetCrossingFailureReason",
    "M:LibreMetaverse.AgentManager.GetCrossingState",
    "M:LibreMetaverse.AgentManager.GetSimulatorsForObject(LibreMetaverse.UUID)",
    "M:LibreMetaverse.AgentManager.IsCrossing",
    "M:LibreMetaverse.AgentManager.IsNearBorder(LibreMetaverse.Vector3,System.UInt32,System.UInt32,System.Single)",
    "M:LibreMetaverse.AgentManager.Jump(System.Boolean)",
    "M:LibreMetaverse.AgentManager.RequestSit(LibreMetaverse.UUID,LibreMetaverse.Vector3)",
    "M:LibreMetaverse.AgentManager.RequestTeleport(System.UInt64,LibreMetaverse.Vector3,LibreMetaverse.Vector3,System.Boolean)",
    "M:LibreMetaverse.AgentManager.Sit",
    "M:LibreMetaverse.AgentManager.Stand",
    "M:LibreMetaverse.AgentManager.TeleportAsync(System.UInt64,LibreMetaverse.Vector3,LibreMetaverse.Vector3,System.Threading.CancellationToken)",
    "M:LibreMetaverse.AgentManager.TrackObjectInSimulator(LibreMetaverse.UUID,LibreMetaverse.Simulator)",
    "M:LibreMetaverse.AgentManager.UntrackObjectInSimulator(LibreMetaverse.UUID,LibreMetaverse.Simulator)",
}


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


def main() -> None:
    missing_types = sorted(REQUIRED_TYPES - generate_api_shims.NATIVE_TYPES.keys())
    if missing_types:
        raise SystemExit("issue 60 native types missing: " + ", ".join(missing_types))
    missing_members = sorted(
        REQUIRED_MEMBERS - generate_api_shims.NATIVE_MEMBER_BODIES.keys()
    )
    if missing_members:
        raise SystemExit("issue 60 native members missing: " + ", ".join(missing_members))
    if STUB_RE.search(SOURCE.read_text()):
        raise SystemExit("issue 60 owned Rust stubs remain in agent_movement.rs")

    require_markers(
        SOURCE,
        (
            "libremetaverse-agent-update",
            "libremetaverse-region-crossing",
            "TeleportLocal",
            "AgentMovementComplete",
            "fake_grid_decodes_locomotion_camera_and_autopilot_packets",
            "fake_grids_complete_a_region_crossing_and_raise_the_final_event",
            "multi_simulator_prediction_and_object_visibility_match_reference_rules",
            "teleport_completion_timeout_and_cancellation_are_deterministic",
            "camera_math_preserves_the_reference_frame_axis_mapping",
        ),
    )
    require_markers(
        DOC,
        (
            "ResetControlFlags",
            "condition variable",
            "generation-tagged",
            "GetCrossingFailureReason",
            "loopback UDP",
        ),
    )
    print(
        "issue 60 audit: native movement/camera mappings, teleport and crossing "
        "state machines, deterministic tests, documentation, and fake-grid wire "
        "coverage are present"
    )


if __name__ == "__main__":
    main()
