"""Audio capture template data model."""

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


@dataclass
class AudioCaptureTemplate:
    """Represents an audio capture template configuration."""

    id: str
    name: str
    engine_type: str
    engine_config: Dict[str, Any]
    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,
            "engine_type": self.engine_type,
            "engine_config": self.engine_config,
            "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) -> "AudioCaptureTemplate":
        """Create template from dictionary."""
        return cls(
            id=data["id"],
            name=data["name"],
            engine_type=data["engine_type"],
            engine_config=data.get("engine_config", {}),
            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", []),
        )
