from __future__ import annotations

import importlib.util
from pathlib import Path

import yaml


def test_reference_bracket_step_fixture_exists_and_documents_mock_usage():
    step_path = Path("examples/bracket.step")

    assert step_path.exists()
    text = step_path.read_text(encoding="utf-8-sig")
    assert "ISO-10303-21" in text
    assert "mock-based" in text
    assert "does not parse this content" in text


def test_reference_context_matches_mock_feature_ids():
    context_path = Path("examples/bracket_user_context.yaml")

    assert context_path.exists()
    context = yaml.safe_load(context_path.read_text(encoding="utf-8"))
    assert "feat_hole_pattern_001" in context["protected_features"]
    assert "feat_hole_pattern_001" in context["simulation"]["fixed"]
    assert context["simulation"]["loads"][0]["target"] == "feat_base_plate_001"


def test_reference_docs_exist_and_explain_mock_and_generated_resources():
    reference_doc = Path("docs/reference_scenario_bracket.md")
    walkthrough = Path("docs/demo_walkthrough.md")

    assert reference_doc.exists()
    assert walkthrough.exists()
    reference_text = reference_doc.read_text(encoding="utf-8")
    walkthrough_text = walkthrough.read_text(encoding="utf-8")
    assert "What is mock-based" in reference_text
    assert "What is generated by the system" in reference_text
    assert "No STEP geometry is parsed" in reference_text
    assert "aieng propose-patch" in walkthrough_text
    assert "ai/patches/patch_0001.json" in walkthrough_text
    assert "No solver result is attached" in walkthrough_text


def test_run_reference_demo_script_command_list_is_sane():
    script_path = Path("scripts/run_reference_demo.py")
    assert script_path.exists()

    spec = importlib.util.spec_from_file_location("run_reference_demo", script_path)
    assert spec is not None and spec.loader is not None
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)

    commands = module.command_chain(
        Path("build/bracket_001.aieng"),
        Path("examples/bracket_user_context.yaml"),
        Path("examples/bracket.step"),
        "Reduce mass by 15% while keeping mounting holes unchanged.",
    )
    command_names = [command[0] for command in commands]
    assert command_names == [
        "import-step",
        "extract-topology",
        "recognize-features",
        "apply-context",
        "build-visual-index",
        "build-visual-manifest",
        "build-interface-graph",
        "import-cae-deck",
        "apply-cae-mapping",
        "build-interface-graph",
        "build-object-registry",
        "summarize",
        "propose-patch",
        "export-calculix",
        "update-validation-status",
        "validate",
    ]
    first_interface_graph = command_names.index("build-interface-graph")
    apply_mapping = command_names.index("apply-cae-mapping")
    second_interface_graph = command_names.index("build-interface-graph", first_interface_graph + 1)
    assert first_interface_graph < apply_mapping < second_interface_graph
    assert ["propose-patch", "build/bracket_001.aieng", "--intent", "Reduce mass by 15% while keeping mounting holes unchanged."] in commands
