"""Theme and design token models"""

from datetime import datetime
from enum import Enum
from typing import Any, Dict, Optional
from uuid import uuid4
from pydantic import BaseModel, Field, ConfigDict


class TokenCategory(str, Enum):
    """Categories of design tokens"""
    COLOR = "color"
    SPACING = "spacing"
    TYPOGRAPHY = "typography"
    RADIUS = "radius"
    SHADOW = "shadow"
    BORDER = "border"
    OTHER = "other"


class DesignToken(BaseModel):
    """A single design token with value and metadata"""
    model_config = ConfigDict(arbitrary_types_allowed=True)

    uuid: str = Field(default_factory=lambda: str(uuid4()), description="UUID for export/import")
    name: str = Field(..., description="Token name (e.g., 'primary', 'space-md')")
    value: Any = Field(..., description="Token value (can be string, number, object)")
    type: str = Field(..., description="Token type (color, dimension, etc.)")
    category: TokenCategory = Field(default=TokenCategory.OTHER, description="Token category")
    description: Optional[str] = Field(None, description="Human-readable description")
    source: Optional[str] = Field(None, description="Source attribution (e.g., 'figma:abc123')")
    deprecated: bool = Field(default=False, description="Is this token deprecated?")
    created_at: datetime = Field(default_factory=datetime.utcnow, description="Creation timestamp")
    updated_at: datetime = Field(default_factory=datetime.utcnow, description="Update timestamp")


class Theme(BaseModel):
    """Complete theme configuration"""
    model_config = ConfigDict(arbitrary_types_allowed=True)

    uuid: str = Field(default_factory=lambda: str(uuid4()), description="UUID for export/import")
    name: str = Field(..., description="Theme name")
    version: str = Field(default="1.0.0", description="Theme version")
    tokens: Dict[str, DesignToken] = Field(default_factory=dict, description="All design tokens")
    created_at: datetime = Field(default_factory=datetime.utcnow, description="Creation timestamp")
    updated_at: datetime = Field(default_factory=datetime.utcnow, description="Update timestamp")

    def get_tokens_by_category(self, category: TokenCategory) -> Dict[str, DesignToken]:
        """Filter tokens by category"""
        return {
            name: token
            for name, token in self.tokens.items()
            if token.category == category
        }
