"""Tests for scenario management."""

import pytest

from fwauto.scenario import Scenario, ScenarioManager


def test_scenario_from_dict():
    """Test Scenario.from_dict creates object correctly."""
    data = {
        "name": "led-on",
        "description": "Turn on LED",
        "binary_args": "on",
    }

    scenario = Scenario.from_dict(data)

    assert scenario.name == "led-on"
    assert scenario.description == "Turn on LED"
    assert scenario.binary_args == "on"


def test_scenario_from_dict_with_defaults():
    """Test Scenario.from_dict handles missing optional fields."""
    data = {"name": "test"}

    scenario = Scenario.from_dict(data)

    assert scenario.name == "test"
    assert scenario.description == ""
    assert scenario.binary_args == ""


def test_scenario_from_dict_missing_name():
    """Test Scenario.from_dict raises error when name is missing."""
    data = {"description": "Test", "binary_args": "test"}

    with pytest.raises(KeyError):
        Scenario.from_dict(data)


def test_scenario_to_dict():
    """Test Scenario.to_dict serializes correctly."""
    scenario = Scenario(
        name="test-scenario",
        description="Test description",
        binary_args="--mode test",
    )

    data = scenario.to_dict()

    assert data == {
        "name": "test-scenario",
        "description": "Test description",
        "binary_args": "--mode test",
    }


def test_scenario_manager_get_scenario():
    """Test ScenarioManager.get_scenario returns correct scenario."""
    scenarios = [
        Scenario("led-on", "Turn on LED", "on"),
        Scenario("led-off", "Turn off LED", "off"),
    ]

    manager = ScenarioManager(scenarios)

    scenario = manager.get_scenario("led-on")
    assert scenario is not None
    assert scenario.name == "led-on"
    assert scenario.binary_args == "on"


def test_scenario_manager_get_scenario_not_found():
    """Test ScenarioManager.get_scenario returns None for missing scenario."""
    scenarios = [Scenario("led-on", "Turn on LED", "on")]

    manager = ScenarioManager(scenarios)

    scenario = manager.get_scenario("nonexistent")
    assert scenario is None


def test_scenario_manager_list_scenarios():
    """Test ScenarioManager.list_scenarios returns all scenarios."""
    scenarios = [
        Scenario("led-on", "Turn on LED", "on"),
        Scenario("led-off", "Turn off LED", "off"),
        Scenario("debug", "Debug mode", "--debug"),
    ]

    manager = ScenarioManager(scenarios)

    all_scenarios = manager.list_scenarios()

    assert len(all_scenarios) == 3
    assert all(isinstance(s, Scenario) for s in all_scenarios)
    scenario_names = [s.name for s in all_scenarios]
    assert "led-on" in scenario_names
    assert "led-off" in scenario_names
    assert "debug" in scenario_names


def test_scenario_manager_empty():
    """Test ScenarioManager works with empty scenario list."""
    manager = ScenarioManager([])

    assert manager.list_scenarios() == []
    assert manager.get_scenario("anything") is None
