"""Tests for built-in effect presets."""

import pytest

from wled_controller.core.game_integration.presets import (
    EffectPreset,
    get_all_presets,
    get_preset,
)
from wled_controller.storage.game_integration import EventMapping


class TestPresetData:
    """Verify preset data structure and contents."""

    def test_get_all_presets_returns_list(self):
        presets = get_all_presets()
        assert isinstance(presets, list)
        assert len(presets) >= 4

    def test_each_preset_has_required_fields(self):
        for preset in get_all_presets():
            assert isinstance(preset, EffectPreset)
            assert preset.key
            assert preset.name
            assert preset.description
            assert len(preset.target_game_types) > 0
            assert len(preset.event_mappings) > 0

    def test_each_mapping_is_valid(self):
        for preset in get_all_presets():
            for mapping in preset.event_mappings:
                assert isinstance(mapping, EventMapping)
                assert mapping.event_type
                assert mapping.effect
                assert len(mapping.color) == 3
                assert all(0 <= c <= 255 for c in mapping.color)
                assert mapping.duration_ms > 0
                assert 0.0 <= mapping.intensity <= 1.0
                assert mapping.priority >= 0

    def test_get_preset_by_key(self):
        assert get_preset("fps_combat") is not None
        assert get_preset("moba_health") is not None
        assert get_preset("racing") is not None
        assert get_preset("generic_alert") is not None

    def test_get_preset_unknown_returns_none(self):
        assert get_preset("nonexistent") is None

    def test_fps_combat_has_expected_events(self):
        preset = get_preset("fps_combat")
        assert preset is not None
        event_types = {m.event_type for m in preset.event_mappings}
        assert "health" in event_types
        assert "kill" in event_types
        assert "death" in event_types

    def test_moba_health_has_expected_events(self):
        preset = get_preset("moba_health")
        assert preset is not None
        event_types = {m.event_type for m in preset.event_mappings}
        assert "health" in event_types
        assert "mana" in event_types

    def test_presets_are_frozen(self):
        preset = get_preset("fps_combat")
        assert preset is not None
        with pytest.raises(AttributeError):
            preset.name = "Changed"  # type: ignore[misc]

    def test_preset_keys_unique(self):
        presets = get_all_presets()
        keys = [p.key for p in presets]
        assert len(keys) == len(set(keys))
