"""Synchronization clock data model.

A SyncClock provides a shared, controllable time base for animation-based
color strip sources.  Multiple CSS sources referencing the same clock
animate in sync and share speed / pause / resume / reset controls.
"""

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


@dataclass
class SyncClock:
    """Persistent configuration for a synchronization clock."""

    id: str
    name: str
    speed: float  # animation speed multiplier (0.1–10.0)
    created_at: datetime
    updated_at: datetime
    description: Optional[str] = None
    tags: List[str] = field(default_factory=list)

    def to_dict(self) -> dict:
        return {
            "id": self.id,
            "name": self.name,
            "speed": self.speed,
            "description": self.description,
            "tags": self.tags,
            "created_at": self.created_at.isoformat(),
            "updated_at": self.updated_at.isoformat(),
        }

    @staticmethod
    def from_dict(data: dict) -> "SyncClock":
        return SyncClock(
            id=data["id"],
            name=data["name"],
            speed=float(data.get("speed", 1.0)),
            description=data.get("description"),
            tags=data.get("tags", []),
            created_at=datetime.fromisoformat(data["created_at"]),
            updated_at=datetime.fromisoformat(data["updated_at"]),
        )
