from __future__ import annotations

from kicad_mcp.capabilities import (
    AccessTier,
    RuntimeRequirement,
    all_records,
    get,
    is_allowed,
    tools_for_profile,
)

REQUIRED_OASLANA_119_TOOLS = {
    "pcb_place_component",
    "pcb_route_trace",
    "pcb_add_zone",
    "pcb_set_design_rules",
    "pcb_move_component",
    "pcb_delete_object",
    "sch_add_component",
    "sch_add_wire",
    "sch_modify_property",
}


def test_is_allowed_for_known_tools_and_profiles() -> None:
    assert is_allowed("kicad_set_project", "minimal") is True
    assert is_allowed("sch_add_symbol", "schematic_only") is True
    assert is_allowed("sch_add_symbol", "minimal") is False
    assert is_allowed("pcb_add_track", "pcb_only") is True
    assert is_allowed("missing_tool", "agent_full") is False


def test_minimal_profile_has_expected_core_tools() -> None:
    records = tools_for_profile("minimal")

    assert records
    names = {record.name for record in records}
    assert {
        "kicad_set_project",
        "kicad_health",
        "kicad_doctor",
        "kicad_get_project_info",
        "pcb_get_board_summary",
        "export_gerber",
    }.issubset(names)
    # Minimal profile can include non-read tiers, but should remain compact.
    assert len(records) < 100


def test_manufacturing_package_requires_human_gate() -> None:
    record = get("export_manufacturing_package")

    assert record is not None
    assert record.human_gate_required is True
    assert record.tier == AccessTier.HUMAN_ONLY
    assert record.supports_dry_run is True


def test_registered_tools_have_valid_verification_levels() -> None:
    valid_levels = {"verified", "experimental", "planned"}

    assert all_records()
    for record in all_records().values():
        assert record.verification_level in valid_levels


def test_oaslana_119_live_editing_tools_require_kicad_ipc() -> None:
    records = all_records()

    assert REQUIRED_OASLANA_119_TOOLS.issubset(records)
    for tool_name in REQUIRED_OASLANA_119_TOOLS:
        record = records[tool_name]
        assert record.runtime is RuntimeRequirement.KICAD_IPC
        assert record.tier is AccessTier.WRITE


def test_profile_results_are_copies() -> None:
    records = all_records()
    records.clear()

    assert all_records()
