"""Built-in effect presets for game integrations.

Presets are read-only template configurations that can be applied to any
game integration. Each preset ships a curated set of event-to-effect
mappings tuned for a specific genre (FPS, MOBA, racing, generic).

Users can apply a preset via API, which merges the preset's mappings into
the integration's event_mappings list. After applying, mappings can be
freely edited.
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import List

from wled_controller.storage.game_integration import EventMapping


@dataclass(frozen=True)
class EffectPreset:
    """A built-in effect preset.

    Attributes:
        key: Unique identifier for the preset (e.g. 'fps_combat').
        name: Display name (e.g. 'FPS Combat').
        description: One-line description of what the preset does.
        target_game_types: Genre tags indicating which games this suits.
        event_mappings: List of pre-configured event-to-effect mappings.
    """

    key: str
    name: str
    description: str
    target_game_types: List[str] = field(default_factory=list)
    event_mappings: List[EventMapping] = field(default_factory=list)


# ── Built-in Presets ────────────────────────────────────────────────────

_PRESETS: dict[str, EffectPreset] = {}


def _reg(preset: EffectPreset) -> None:
    """Register a preset in the module-level registry."""
    _PRESETS[preset.key] = preset


_reg(
    EffectPreset(
        key="fps_combat",
        name="FPS Combat",
        description="Health glow, kill flash, death pulse, round start sweep",
        target_game_types=["fps"],
        event_mappings=[
            EventMapping(
                event_type="health",
                effect="breathing",
                color=[255, 50, 50],
                duration_ms=2000,
                intensity=0.6,
                priority=3,
            ),
            EventMapping(
                event_type="kill",
                effect="flash",
                color=[0, 255, 0],
                duration_ms=400,
                intensity=1.0,
                priority=8,
            ),
            EventMapping(
                event_type="death",
                effect="pulse",
                color=[255, 0, 0],
                duration_ms=1500,
                intensity=1.0,
                priority=10,
            ),
            EventMapping(
                event_type="round_start",
                effect="sweep",
                color=[0, 100, 255],
                duration_ms=800,
                intensity=0.8,
                priority=5,
            ),
        ],
    )
)

_reg(
    EffectPreset(
        key="moba_health",
        name="MOBA Health",
        description="Health gradient green-yellow-red, mana blue glow, death fade to black",
        target_game_types=["moba"],
        event_mappings=[
            EventMapping(
                event_type="health",
                effect="color_shift",
                color=[0, 255, 0],
                duration_ms=1000,
                intensity=0.7,
                priority=4,
            ),
            EventMapping(
                event_type="mana",
                effect="breathing",
                color=[50, 100, 255],
                duration_ms=2000,
                intensity=0.5,
                priority=3,
            ),
            EventMapping(
                event_type="death",
                effect="pulse",
                color=[20, 20, 20],
                duration_ms=2500,
                intensity=1.0,
                priority=10,
            ),
        ],
    )
)

_reg(
    EffectPreset(
        key="racing",
        name="Racing",
        description="Speed-based color temperature, boost rainbow flash",
        target_game_types=["racing"],
        event_mappings=[
            EventMapping(
                event_type="speed",
                effect="color_shift",
                color=[255, 120, 0],
                duration_ms=500,
                intensity=0.8,
                priority=4,
            ),
            EventMapping(
                event_type="buffed",
                effect="flash",
                color=[128, 0, 255],
                duration_ms=300,
                intensity=1.0,
                priority=9,
            ),
        ],
    )
)

_reg(
    EffectPreset(
        key="generic_alert",
        name="Generic Alert",
        description="White flash on any trigger event",
        target_game_types=["any"],
        event_mappings=[
            EventMapping(
                event_type="kill",
                effect="flash",
                color=[255, 255, 255],
                duration_ms=400,
                intensity=1.0,
                priority=5,
            ),
            EventMapping(
                event_type="death",
                effect="flash",
                color=[255, 255, 255],
                duration_ms=400,
                intensity=1.0,
                priority=5,
            ),
            EventMapping(
                event_type="round_start",
                effect="flash",
                color=[255, 255, 255],
                duration_ms=400,
                intensity=1.0,
                priority=5,
            ),
            EventMapping(
                event_type="objective_captured",
                effect="flash",
                color=[255, 255, 255],
                duration_ms=400,
                intensity=1.0,
                priority=5,
            ),
        ],
    )
)


# ── Public API ──────────────────────────────────────────────────────────


def get_all_presets() -> list[EffectPreset]:
    """Return all built-in presets."""
    return list(_PRESETS.values())


def get_preset(key: str) -> EffectPreset | None:
    """Look up a preset by key."""
    return _PRESETS.get(key)
