"""
DSS Core Configuration

Defines the canonical DSS design system reference.
DSS "eats its own dog food" - using shadcn/ui as the base layer.

Hierarchy:
1. DSS Core (shadcn/ui from Figma) - immutable base
2. Skins - themed variations (material, ant, custom)
3. Projects - customer customizations
"""

from dataclasses import dataclass
from pathlib import Path
from typing import Optional


# =============================================================================
# DSS CANONICAL FIGMA REFERENCE
# =============================================================================

@dataclass(frozen=True)
class DSSFigmaReference:
    """Immutable reference to DSS's canonical Figma source."""
    team_id: str = "857274453634536756"
    team_name: str = "bruno.sarlo.uy"
    project_id: str = "10864574"
    project_name: str = "DSS"
    uikit_file_key: str = "evCZlaeZrP7X20NIViSJbl"
    uikit_file_name: str = "Obra shadcn/ui (Community)"


# Singleton instance - THE canonical DSS Figma reference
DSS_FIGMA_REFERENCE = DSSFigmaReference()


# =============================================================================
# DSS CORE PATHS
# =============================================================================

# DSS installation paths
DSS_ROOT = Path("/home/overbits/dss")
DSS_MVP1 = DSS_ROOT / "dss-mvp1"
DSS_CORE_DIR = DSS_MVP1 / "dss" / "core_tokens"

# User data paths
DSS_USER_DIR = Path.home() / ".dss"
DSS_CACHE_DIR = DSS_USER_DIR / "cache"
DSS_REGISTRY_FILE = DSS_USER_DIR / "registry.json"


# =============================================================================
# DSS CORE TOKENS STRUCTURE
# =============================================================================

DSS_CORE_TOKEN_CATEGORIES = {
    "colors": {
        "description": "Color palette based on shadcn/ui",
        "includes": [
            "background", "foreground", "card", "popover", "primary",
            "secondary", "muted", "accent", "destructive", "border",
            "input", "ring", "chart"
        ]
    },
    "typography": {
        "description": "Typography scale from shadcn/ui",
        "includes": [
            "heading-1", "heading-2", "heading-3", "heading-4",
            "paragraph-large", "paragraph-small", "label", "caption"
        ]
    },
    "spacing": {
        "description": "Spacing scale",
        "includes": ["0", "1", "2", "3", "4", "5", "6", "8", "10", "12", "16", "20", "24"]
    },
    "radius": {
        "description": "Border radius values",
        "includes": ["none", "sm", "md", "lg", "xl", "full"]
    },
    "shadows": {
        "description": "Shadow/elevation scale",
        "includes": ["none", "sm", "md", "lg", "xl", "2xl", "inner"]
    }
}


# =============================================================================
# DSS CORE COMPONENTS
# =============================================================================

DSS_CORE_COMPONENTS = {
    # Primitives
    "Button": {"variants": ["default", "destructive", "outline", "secondary", "ghost", "link"]},
    "Input": {"variants": ["default", "file"]},
    "Textarea": {"variants": ["default"]},
    "Select": {"variants": ["default"]},
    "Checkbox": {"variants": ["default"]},
    "Radio": {"variants": ["default"]},
    "Switch": {"variants": ["default"]},
    "Slider": {"variants": ["default"]},
    "Toggle": {"variants": ["default", "outline"]},

    # Layout
    "Card": {"variants": ["default"]},
    "Separator": {"variants": ["default"]},
    "AspectRatio": {"variants": ["default"]},
    "ScrollArea": {"variants": ["default"]},

    # Data Display
    "Avatar": {"variants": ["default"]},
    "Badge": {"variants": ["default", "secondary", "destructive", "outline"]},
    "Table": {"variants": ["default"]},

    # Feedback
    "Alert": {"variants": ["default", "destructive"]},
    "AlertDialog": {"variants": ["default"]},
    "Progress": {"variants": ["default"]},
    "Skeleton": {"variants": ["default"]},
    "Toast": {"variants": ["default", "destructive"]},
    "Tooltip": {"variants": ["default"]},

    # Overlay
    "Dialog": {"variants": ["default"]},
    "Drawer": {"variants": ["default"]},
    "Popover": {"variants": ["default"]},
    "DropdownMenu": {"variants": ["default"]},
    "ContextMenu": {"variants": ["default"]},
    "Sheet": {"variants": ["default"]},
    "HoverCard": {"variants": ["default"]},

    # Navigation
    "Tabs": {"variants": ["default"]},
    "NavigationMenu": {"variants": ["default"]},
    "Breadcrumb": {"variants": ["default"]},
    "Pagination": {"variants": ["default"]},
    "Menubar": {"variants": ["default"]},

    # Form
    "Form": {"variants": ["default"]},
    "Label": {"variants": ["default"]},
    "Calendar": {"variants": ["default"]},
    "DatePicker": {"variants": ["default"]},
    "Combobox": {"variants": ["default"]},

    # Data
    "DataTable": {"variants": ["default"]},
    "Command": {"variants": ["default"]},

    # Layout Containers
    "Accordion": {"variants": ["default"]},
    "Collapsible": {"variants": ["default"]},
    "Carousel": {"variants": ["default"]},
    "Resizable": {"variants": ["default"]},
}


# =============================================================================
# DSS CORE THEMES
# =============================================================================

DSS_CORE_THEMES = {
    "light": {
        "description": "Default light theme based on shadcn/ui zinc",
        "colors": {
            "background": "0 0% 100%",
            "foreground": "240 10% 3.9%",
            "card": "0 0% 100%",
            "card-foreground": "240 10% 3.9%",
            "popover": "0 0% 100%",
            "popover-foreground": "240 10% 3.9%",
            "primary": "240 5.9% 10%",
            "primary-foreground": "0 0% 98%",
            "secondary": "240 4.8% 95.9%",
            "secondary-foreground": "240 5.9% 10%",
            "muted": "240 4.8% 95.9%",
            "muted-foreground": "240 3.8% 46.1%",
            "accent": "240 4.8% 95.9%",
            "accent-foreground": "240 5.9% 10%",
            "destructive": "0 84.2% 60.2%",
            "destructive-foreground": "0 0% 98%",
            "border": "240 5.9% 90%",
            "input": "240 5.9% 90%",
            "ring": "240 5.9% 10%",
        }
    },
    "dark": {
        "description": "Default dark theme based on shadcn/ui zinc",
        "colors": {
            "background": "240 10% 3.9%",
            "foreground": "0 0% 98%",
            "card": "240 10% 3.9%",
            "card-foreground": "0 0% 98%",
            "popover": "240 10% 3.9%",
            "popover-foreground": "0 0% 98%",
            "primary": "0 0% 98%",
            "primary-foreground": "240 5.9% 10%",
            "secondary": "240 3.7% 15.9%",
            "secondary-foreground": "0 0% 98%",
            "muted": "240 3.7% 15.9%",
            "muted-foreground": "240 5% 64.9%",
            "accent": "240 3.7% 15.9%",
            "accent-foreground": "0 0% 98%",
            "destructive": "0 62.8% 30.6%",
            "destructive-foreground": "0 0% 98%",
            "border": "240 3.7% 15.9%",
            "input": "240 3.7% 15.9%",
            "ring": "240 4.9% 83.9%",
        }
    }
}


# =============================================================================
# HELPER FUNCTIONS
# =============================================================================

def get_dss_figma_reference() -> DSSFigmaReference:
    """Get the canonical DSS Figma reference."""
    return DSS_FIGMA_REFERENCE


def get_core_token_path(category: str) -> Optional[Path]:
    """Get path to core token file for a category."""
    if category not in DSS_CORE_TOKEN_CATEGORIES:
        return None
    return DSS_CORE_DIR / f"{category}.json"


def ensure_dss_directories():
    """Ensure DSS system directories exist."""
    DSS_USER_DIR.mkdir(parents=True, exist_ok=True)
    DSS_CACHE_DIR.mkdir(parents=True, exist_ok=True)
    DSS_CORE_DIR.mkdir(parents=True, exist_ok=True)


def is_dss_core_component(name: str) -> bool:
    """Check if a component is part of DSS core."""
    return name in DSS_CORE_COMPONENTS


def get_component_variants(name: str) -> list:
    """Get variants for a DSS core component."""
    comp = DSS_CORE_COMPONENTS.get(name, {})
    return comp.get("variants", [])
