"""
Preset pack format definition.

Defines the structure of preset packs and their TOML-based configuration.
"""

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


@dataclass
class PresetPackMetadata:
    """Metadata for a preset pack."""

    name: str
    version: str
    author: str
    description: str
    sideline_version: str  # Compatible Sideline version
    created: Optional[str] = None  # ISO 8601 timestamp
    tags: List[str] = field(default_factory=list)

    def to_dict(self) -> Dict:
        """Convert to dictionary for TOML serialization."""
        return {
            "name": self.name,
            "version": self.version,
            "author": self.author,
            "description": self.description,
            "sideline_version": self.sideline_version,
            "created": self.created,
            "tags": self.tags,
        }

    @classmethod
    def from_dict(cls, data: Dict) -> "PresetPackMetadata":
        """Create from dictionary."""
        return cls(
            name=data["name"],
            version=data["version"],
            author=data["author"],
            description=data["description"],
            sideline_version=data["sideline_version"],
            created=data.get("created"),
            tags=data.get("tags", []),
        )


@dataclass
class PluginEntry:
    """Entry for a plugin in the preset pack."""

    name: str
    category: str  # source, effect, display, camera
    encoded_code: str  # ASCII art encoded plugin code
    permissions: List[str] = field(default_factory=list)
    capabilities: List[str] = field(default_factory=list)

    def to_dict(self) -> Dict:
        """Convert to dictionary for TOML serialization."""
        return {
            "name": self.name,
            "category": self.category,
            "code": self.encoded_code,
            "permissions": self.permissions,
            "capabilities": self.capabilities,
        }

    @classmethod
    def from_dict(cls, data: Dict) -> "PluginEntry":
        """Create from dictionary."""
        return cls(
            name=data["name"],
            category=data["category"],
            encoded_code=data["code"],
            permissions=data.get("permissions", []),
            capabilities=data.get("capabilities", []),
        )


@dataclass
class PresetEntry:
    """Entry for a preset in the preset pack."""

    name: str
    config: Dict  # Preset configuration (TOML-compatible)

    def to_dict(self) -> Dict:
        """Convert to dictionary for TOML serialization."""
        return {
            "name": self.name,
            "config": self.config,
        }

    @classmethod
    def from_dict(cls, data: Dict) -> "PresetEntry":
        """Create from dictionary."""
        return cls(
            name=data["name"],
            config=data["config"],
        )


@dataclass
class PresetPack:
    """Complete preset pack with metadata, plugins, and presets."""

    metadata: PresetPackMetadata
    plugins: List[PluginEntry] = field(default_factory=list)
    presets: List[PresetEntry] = field(default_factory=list)

    def to_dict(self) -> Dict:
        """Convert to dictionary for TOML serialization."""
        return {
            "pack": self.metadata.to_dict(),
            "plugins": [p.to_dict() for p in self.plugins],
            "presets": [p.to_dict() for p in self.presets],
        }

    @classmethod
    def from_dict(cls, data: Dict) -> "PresetPack":
        """Create from dictionary."""
        return cls(
            metadata=PresetPackMetadata.from_dict(data["pack"]),
            plugins=[PluginEntry.from_dict(p) for p in data.get("plugins", [])],
            presets=[PresetEntry.from_dict(p) for p in data.get("presets", [])],
        )
