"""Category definitions for organizing Ghostty configuration options."""

from typing import Final

# Category prefix mappings (check these first)
CATEGORY_PREFIXES: Final[dict[str, str]] = {
    # Font
    "font": "Font",
    "bold": "Font",
    "italic": "Font",
    "freetype": "Font",
    # Cursor
    "cursor": "Cursor",
    # Window
    "window": "Window",
    "resize": "Window",
    "initial": "Window",
    "fullscreen": "Window",
    "maximize": "Window",
    "title": "Window",
    "split": "Window",
    "tab": "Window",
    # Mouse
    "mouse": "Mouse",
    "focus-follows": "Mouse",
    # Clipboard
    "clipboard": "Clipboard",
    "copy": "Clipboard",
    "paste": "Clipboard",
    "selection": "Clipboard",
    # Shell
    "shell": "Shell",
    "working": "Shell",
    "command": "Shell",
    "env": "Shell",
    "input": "Shell",
    "notify": "Shell",
    "wait": "Shell",
    "abnormal": "Shell",
    # Scrollback
    "scrollback": "Scrollback",
    "scroll": "Scrollback",
    # Keybindings
    "keybind": "Keybindings",
    "key-remap": "Keybindings",
    # Colours
    "background": "Colours",
    "foreground": "Colours",
    "palette": "Colours",
    "search": "Colours",
    "unfocused-split": "Colours",
    # Theme
    "theme": "Theme",
    "alpha": "Theme",
    # Links
    "link": "Links",
    "url": "Links",
    # Behaviour
    "confirm": "Behaviour",
    "quit": "Behaviour",
    "focus": "Behaviour",
    "bell": "Behaviour",
    "audible": "Behaviour",
    "visual": "Behaviour",
    "request": "Behaviour",
    "language": "Behaviour",
    # macOS
    "macos": "macOS",
    # GTK/Linux
    "gtk": "GTK/Linux",
    "class": "GTK/Linux",
    # X11
    "x11": "X11",
    # Wayland
    "wayland": "Wayland",
    # Terminal
    "term": "Terminal",
    "osc": "Terminal",
    "grapheme": "Terminal",
    # Advanced
    "config": "Advanced",
    "auto": "Advanced",
    "live": "Advanced",
    "minimum": "Advanced",
    "adjust": "Advanced",
    "custom-shader": "Advanced",
    "debug": "Advanced",
    "force": "Advanced",
    "renderer": "Advanced",
    "log": "Advanced",
}

# Display order for categories
CATEGORY_ORDER: Final[list[str]] = [
    "Font",
    "Colours",
    "Theme",
    "Cursor",
    "Window",
    "Mouse",
    "Clipboard",
    "Scrollback",
    "Shell",
    "Terminal",
    "Keybindings",
    "Links",
    "Behaviour",
    "macOS",
    "GTK/Linux",
    "X11",
    "Wayland",
    "Advanced",
    "Other",
]

# Category display info with icons (using standard emoji)
CATEGORY_INFO: Final[dict[str, dict[str, str]]] = {
    "Font": {"icon": "🔤", "description": "Font family, size, and style settings"},
    "Colours": {"icon": "🎨", "description": "Colour scheme and palette settings"},
    "Theme": {"icon": "🎭", "description": "Theme and appearance settings"},
    "Cursor": {"icon": "📍", "description": "Cursor style and behaviour"},
    "Window": {"icon": "🪟", "description": "Window size and appearance"},
    "Mouse": {"icon": "🖱️", "description": "Mouse behaviour settings"},
    "Clipboard": {"icon": "📋", "description": "Copy and paste settings"},
    "Scrollback": {"icon": "📜", "description": "Scrollback buffer settings"},
    "Shell": {"icon": "🐚", "description": "Shell and command settings"},
    "Terminal": {"icon": "💻", "description": "Terminal behaviour settings"},
    "Keybindings": {"icon": "⌨️", "description": "Keyboard shortcuts"},
    "Links": {"icon": "🔗", "description": "URL and link handling"},
    "Behaviour": {"icon": "⚙️", "description": "General behaviour settings"},
    "macOS": {"icon": "🍎", "description": "macOS-specific settings"},
    "GTK/Linux": {"icon": "🐧", "description": "GTK/Linux-specific settings"},
    "X11": {"icon": "🖥️", "description": "X11-specific settings"},
    "Wayland": {"icon": "🌊", "description": "Wayland-specific settings"},
    "Advanced": {"icon": "🔧", "description": "Advanced configuration"},
    "Other": {"icon": "📦", "description": "Miscellaneous settings"},
}


def infer_category(option_name: str) -> str:
    """Infer category from option name using prefix matching."""
    name_lower = option_name.lower()

    # Check prefix matches
    for prefix, category in CATEGORY_PREFIXES.items():
        if name_lower.startswith(prefix):
            return category

    # Check if any keyword is contained in the name
    for keyword, category in CATEGORY_PREFIXES.items():
        if keyword in name_lower:
            return category

    return "Other"


def get_category_display_name(category: str) -> str:
    """Get display name for a category."""
    if category in CATEGORY_INFO:
        return category
    return "Other"


def get_category_icon(category: str) -> str:
    """Get icon for a category."""
    info = CATEGORY_INFO.get(category, CATEGORY_INFO["Other"])
    return info["icon"]


def get_category_description(category: str) -> str:
    """Get description for a category."""
    info = CATEGORY_INFO.get(category, CATEGORY_INFO["Other"])
    return info["description"]


def get_sorted_categories(categories: set[str]) -> list[str]:
    """Return categories sorted by display order."""
    ordered = [c for c in CATEGORY_ORDER if c in categories]
    remaining = sorted(categories - set(ordered))
    return ordered + remaining


def get_all_categories() -> list[str]:
    """Get all available categories in display order."""
    return list(CATEGORY_ORDER)
