"""Scene preset data models — snapshot of target state."""

from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import List


@dataclass
class TargetSnapshot:
    """Snapshot of a single target's mutable state."""

    target_id: str
    running: bool = False
    color_strip_source_id: str = ""
    brightness_value_source_id: str = ""
    fps: int = 30

    def to_dict(self) -> dict:
        return {
            "target_id": self.target_id,
            "running": self.running,
            "color_strip_source_id": self.color_strip_source_id,
            "brightness_value_source_id": self.brightness_value_source_id,
            "fps": self.fps,
        }

    @classmethod
    def from_dict(cls, data: dict) -> "TargetSnapshot":
        return cls(
            target_id=data["target_id"],
            running=data.get("running", False),
            color_strip_source_id=data.get("color_strip_source_id", ""),
            brightness_value_source_id=data.get("brightness_value_source_id", ""),
            fps=data.get("fps", 30),
        )


@dataclass
class ScenePreset:
    """A named snapshot of target state that can be restored."""

    id: str
    name: str
    description: str = ""
    tags: List[str] = field(default_factory=list)
    targets: List[TargetSnapshot] = field(default_factory=list)
    order: int = 0
    created_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
    updated_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))

    def to_dict(self) -> dict:
        return {
            "id": self.id,
            "name": self.name,
            "description": self.description,
            "tags": self.tags,
            "targets": [t.to_dict() for t in self.targets],
            "order": self.order,
            "created_at": self.created_at.isoformat(),
            "updated_at": self.updated_at.isoformat(),
        }

    @classmethod
    def from_dict(cls, data: dict) -> "ScenePreset":
        return cls(
            id=data["id"],
            name=data["name"],
            description=data.get("description", ""),
            tags=data.get("tags", []),
            targets=[TargetSnapshot.from_dict(t) for t in data.get("targets", [])],
            order=data.get("order", 0),
            created_at=datetime.fromisoformat(data.get("created_at", datetime.now(timezone.utc).isoformat())),
            updated_at=datetime.fromisoformat(data.get("updated_at", datetime.now(timezone.utc).isoformat())),
        )
