"""
DSS Canonical Structure Definitions

Defines the immutable DSS canonical token and component structure.
These definitions are used for validation and auto-completion.
"""

from typing import Dict, List, Set

# DSS Canonical Token Paths
# These are the core tokens that DSS defines
DSS_CANONICAL_TOKENS: Set[str] = {
    # Colors - Primary
    "color.primary.50",
    "color.primary.100",
    "color.primary.200",
    "color.primary.300",
    "color.primary.400",
    "color.primary.500",
    "color.primary.600",
    "color.primary.700",
    "color.primary.800",
    "color.primary.900",
    # Colors - Secondary
    "color.secondary.50",
    "color.secondary.100",
    "color.secondary.200",
    "color.secondary.300",
    "color.secondary.400",
    "color.secondary.500",
    "color.secondary.600",
    "color.secondary.700",
    "color.secondary.800",
    "color.secondary.900",
    # Colors - Neutral
    "color.neutral.50",
    "color.neutral.100",
    "color.neutral.200",
    "color.neutral.300",
    "color.neutral.400",
    "color.neutral.500",
    "color.neutral.600",
    "color.neutral.700",
    "color.neutral.800",
    "color.neutral.900",
    # Colors - Semantic
    "color.success.500",
    "color.warning.500",
    "color.danger.500",
    "color.info.500",
    "color.accent.500",
    # Colors - Surface
    "color.background",
    "color.foreground",
    "color.muted",
    "color.border",
    "color.ring",
    # Spacing
    "spacing.xs",
    "spacing.sm",
    "spacing.md",
    "spacing.lg",
    "spacing.xl",
    "spacing.2xl",
    "spacing.base",
    # Typography - Size
    "typography.size.xs",
    "typography.size.sm",
    "typography.size.base",
    "typography.size.lg",
    "typography.size.xl",
    "typography.size.2xl",
    "typography.size.3xl",
    "typography.size.4xl",
    # Typography - Weight
    "typography.weight.light",
    "typography.weight.normal",
    "typography.weight.medium",
    "typography.weight.semibold",
    "typography.weight.bold",
    # Typography - Line Height
    "typography.lineHeight.tight",
    "typography.lineHeight.normal",
    "typography.lineHeight.relaxed",
    # Typography - Font Family
    "typography.family.sans",
    "typography.family.serif",
    "typography.family.mono",
    # Border Radius
    "border.radius.none",
    "border.radius.sm",
    "border.radius.md",
    "border.radius.lg",
    "border.radius.xl",
    "border.radius.full",
    # Border Width
    "border.width.none",
    "border.width.thin",
    "border.width.default",
    "border.width.thick",
    # Shadows
    "shadow.none",
    "shadow.sm",
    "shadow.md",
    "shadow.lg",
    "shadow.xl",
    "shadow.inner",
    # Motion - Duration
    "motion.duration.instant",
    "motion.duration.fast",
    "motion.duration.normal",
    "motion.duration.slow",
    # Motion - Easing
    "motion.easing.linear",
    "motion.easing.ease",
    "motion.easing.easeIn",
    "motion.easing.easeOut",
    "motion.easing.easeInOut",
    # Z-Index
    "zIndex.base",
    "zIndex.dropdown",
    "zIndex.sticky",
    "zIndex.fixed",
    "zIndex.modal",
    "zIndex.popover",
    "zIndex.tooltip",
    # Opacity
    "opacity.0",
    "opacity.25",
    "opacity.50",
    "opacity.75",
    "opacity.100",
    # Breakpoints
    "breakpoint.sm",
    "breakpoint.md",
    "breakpoint.lg",
    "breakpoint.xl",
    "breakpoint.2xl",
}

# Commonly used aliases for DSS tokens
DSS_TOKEN_ALIASES: Dict[str, str] = {
    # Color aliases
    "color.primary": "color.primary.500",
    "color.secondary": "color.secondary.500",
    "color.success": "color.success.500",
    "color.warning": "color.warning.500",
    "color.danger": "color.danger.500",
    "color.destructive": "color.danger.500",
    "color.error": "color.danger.500",
    # Spacing aliases
    "space.xs": "spacing.xs",
    "space.sm": "spacing.sm",
    "space.md": "spacing.md",
    "space.lg": "spacing.lg",
    "space.xl": "spacing.xl",
    # Radius aliases
    "radius.sm": "border.radius.sm",
    "radius.md": "border.radius.md",
    "radius.lg": "border.radius.lg",
    # Typography aliases
    "font.size.base": "typography.size.base",
    "font.weight.bold": "typography.weight.bold",
    "lineHeight.normal": "typography.lineHeight.normal",
}

# DSS Canonical Components
DSS_CANONICAL_COMPONENTS: Set[str] = {
    # Primitives
    "Button",
    "Input",
    "Textarea",
    "Select",
    "Checkbox",
    "Radio",
    "RadioGroup",
    "Switch",
    "Slider",
    "Toggle",
    # Layout
    "Box",
    "Flex",
    "Grid",
    "Container",
    "Stack",
    "Spacer",
    "Divider",
    # Data Display
    "Card",
    "Avatar",
    "Badge",
    "Chip",
    "Tag",
    "Icon",
    "Image",
    "Table",
    "List",
    "ListItem",
    # Feedback
    "Alert",
    "Toast",
    "Progress",
    "Spinner",
    "Skeleton",
    "Tooltip",
    # Overlay
    "Modal",
    "Dialog",
    "Drawer",
    "Popover",
    "Dropdown",
    "DropdownMenu",
    "ContextMenu",
    # Navigation
    "Tabs",
    "TabList",
    "Tab",
    "TabPanel",
    "Breadcrumb",
    "Pagination",
    "Menu",
    "MenuItem",
    "NavLink",
    "Link",
    # Typography
    "Text",
    "Heading",
    "Label",
    "Code",
    # Forms
    "Form",
    "FormControl",
    "FormLabel",
    "FormHelperText",
    "FormErrorMessage",
}

# DSS Component Variants
DSS_COMPONENT_VARIANTS: Dict[str, List[str]] = {
    "Button": ["variant", "size", "colorScheme", "isDisabled", "isLoading"],
    "Input": ["variant", "size", "isDisabled", "isInvalid", "isReadOnly"],
    "Card": ["variant", "size", "shadow"],
    "Badge": ["variant", "colorScheme", "size"],
    "Alert": ["status", "variant"],
    "Modal": ["size", "isCentered", "scrollBehavior"],
}

# Valid variant values
DSS_VARIANT_VALUES: Dict[str, Dict[str, List[str]]] = {
    "Button": {
        "variant": ["solid", "outline", "ghost", "link", "unstyled"],
        "size": ["xs", "sm", "md", "lg"],
        "colorScheme": ["primary", "secondary", "success", "warning", "danger"],
    },
    "Input": {
        "variant": ["outline", "filled", "flushed", "unstyled"],
        "size": ["xs", "sm", "md", "lg"],
    },
    "Card": {
        "variant": ["elevated", "outline", "filled", "unstyled"],
        "size": ["sm", "md", "lg"],
    },
}


def get_canonical_token_categories() -> Dict[str, List[str]]:
    """Get tokens organized by category."""
    categories: Dict[str, List[str]] = {}

    for token in DSS_CANONICAL_TOKENS:
        parts = token.split(".")
        category = parts[0]
        if category not in categories:
            categories[category] = []
        categories[category].append(token)

    return categories


def is_valid_dss_token(path: str) -> bool:
    """Check if token path is in canonical structure or valid custom namespace."""
    if path in DSS_CANONICAL_TOKENS:
        return True

    # Check aliases
    if path in DSS_TOKEN_ALIASES:
        return True

    # Check custom namespace
    parts = path.split(".")
    if len(parts) >= 3 and parts[1] in ("brand", "custom"):
        return True

    return False


def resolve_alias(path: str) -> str:
    """Resolve token alias to canonical path."""
    return DSS_TOKEN_ALIASES.get(path, path)
