"""Tests for scenario presets."""

from __future__ import annotations

from pathlib import Path

import pytest

from src.scenarios import BUILTIN_SCENARIOS, Scenario, list_scenarios, load_scenario


AGENTS_DIR = Path("agents")


class TestScenario:
    """Tests for Scenario model."""

    def test_scenario_creation(self) -> None:
        """Scenario stores name, description, agents list."""
        s = Scenario(
            name="test",
            description="A test scenario",
            agents=["architect", "devops"],
        )
        assert s.name == "test"
        assert s.description == "A test scenario"
        assert s.agents == ["architect", "devops"]
        assert s.suggested_max_rounds == 10
        assert s.context_hint == ""

    def test_scenario_with_all_fields(self) -> None:
        """Scenario with all optional fields populated."""
        s = Scenario(
            name="full",
            description="Full scenario",
            agents=["architect"],
            suggested_max_rounds=15,
            context_hint="Provide a product brief",
        )
        assert s.suggested_max_rounds == 15
        assert s.context_hint == "Provide a product brief"


class TestBuiltinScenarios:
    """Tests for built-in scenario presets."""

    def test_builtin_scenarios_not_empty(self) -> None:
        """At least one builtin scenario exists."""
        assert len(BUILTIN_SCENARIOS) > 0

    def test_builtin_scenario_agents_exist(self) -> None:
        """All agents referenced in builtin scenarios exist in agents/ dir."""
        for scenario_name, scenario in BUILTIN_SCENARIOS.items():
            for agent_name in scenario.agents:
                matching = list(AGENTS_DIR.glob(f"{agent_name}.*"))
                assert len(matching) > 0, (
                    f"Agent '{agent_name}' in scenario '{scenario_name}' "
                    f"not found in {AGENTS_DIR}"
                )

    def test_product_review_scenario(self) -> None:
        """product_review scenario exists with expected agents."""
        assert "product_review" in BUILTIN_SCENARIOS
        s = BUILTIN_SCENARIOS["product_review"]
        assert "gstack_commercial" in s.agents
        assert "gstack_technical" in s.agents

    def test_startup_pitch_scenario(self) -> None:
        """startup_pitch scenario has higher max_rounds."""
        assert "startup_pitch" in BUILTIN_SCENARIOS
        s = BUILTIN_SCENARIOS["startup_pitch"]
        assert s.suggested_max_rounds == 12


class TestLoadScenario:
    """Tests for load_scenario function."""

    def test_load_existing_scenario(self) -> None:
        """load_scenario returns the requested scenario."""
        s = load_scenario("product_review")
        assert isinstance(s, Scenario)
        assert s.name == "product_review"

    def test_load_nonexistent_scenario_raises(self) -> None:
        """load_scenario raises KeyError for unknown scenario."""
        with pytest.raises(KeyError):
            load_scenario("nonexistent_scenario")


class TestListScenarios:
    """Tests for list_scenarios function."""

    def test_list_scenarios_returns_all(self) -> None:
        """list_scenarios returns all builtin scenarios."""
        result = list_scenarios()
        assert result == BUILTIN_SCENARIOS
