"""Color strip processing template data model."""

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

from wled_controller.core.filters.filter_instance import FilterInstance


@dataclass
class ColorStripProcessingTemplate:
    """Processing template for color strip sources — ordered list of filters
    applied to 1D LED arrays (N, 3) uint8.
    """

    id: str
    name: str
    filters: List[FilterInstance]
    created_at: datetime
    updated_at: datetime
    description: Optional[str] = None
    tags: List[str] = field(default_factory=list)

    def to_dict(self) -> dict:
        """Convert template to dictionary."""
        return {
            "id": self.id,
            "name": self.name,
            "filters": [f.to_dict() for f in self.filters],
            "created_at": self.created_at.isoformat(),
            "updated_at": self.updated_at.isoformat(),
            "description": self.description,
            "tags": self.tags,
        }

    @classmethod
    def from_dict(cls, data: dict) -> "ColorStripProcessingTemplate":
        """Create template from dictionary."""
        filters = [FilterInstance.from_dict(f) for f in data.get("filters", [])]

        return cls(
            id=data["id"],
            name=data["name"],
            filters=filters,
            created_at=datetime.fromisoformat(data["created_at"])
            if isinstance(data.get("created_at"), str)
            else data.get("created_at", datetime.now(timezone.utc)),
            updated_at=datetime.fromisoformat(data["updated_at"])
            if isinstance(data.get("updated_at"), str)
            else data.get("updated_at", datetime.now(timezone.utc)),
            description=data.get("description"),
            tags=data.get("tags", []),
        )
