"""Built-in theme definitions for sqlit."""

from __future__ import annotations

from rich.style import Style
from textual.theme import Theme
from textual.widgets.text_area import TextAreaTheme

MODE_NORMAL_COLOR_VAR = "mode-normal-color"
MODE_INSERT_COLOR_VAR = "mode-insert-color"

DEFAULT_MODE_COLORS = {
    "dark": {
        MODE_NORMAL_COLOR_VAR: "#D8C499",
        MODE_INSERT_COLOR_VAR: "#91C58D",
    },
    "light": {
        MODE_NORMAL_COLOR_VAR: "#A7B5C3",
        MODE_INSERT_COLOR_VAR: "#86C59B",
    },
}

LIGHT_THEME_NAMES = {
    "sqlit-light",
    "textual-light",
    "solarized-light",
    "catppuccin-latte",
    "rose-pine-dawn",
    "gruvbox-light",
}

SQLIT_THEMES = [
    Theme(
        name="sqlit",
        primary="#97CB93",
        secondary="#6D8DC4",
        accent="#6D8DC4",
        warning="#f59e0b",
        error="#BE728C",
        success="#4ADE80",
        foreground="#a9b1d6",
        background="#1A1B26",
        surface="#24283B",
        panel="#414868",
        dark=True,
        variables={
            "border": "#414868",
            "footer-background": "#24283B",
            "footer-key-foreground": "#7FA1DE",
            "button-color-foreground": "#1A1B26",
            "input-selection-background": "#2a3144 40%",
            MODE_INSERT_COLOR_VAR: "#97CB93",
        },
    ),
    Theme(
        name="sqlit-light",
        primary="#2E7D32",
        secondary="#1565C0",
        accent="#1565C0",
        warning="#F57C00",
        error="#C62828",
        success="#2E7D32",
        foreground="#37474F",
        background="#FAFAFA",
        surface="#FFFFFF",
        panel="#ECEFF1",
        dark=False,
        variables={
            "border": "#B0BEC5",
            "footer-background": "#ECEFF1",
            "footer-key-foreground": "#1565C0",
            "button-color-foreground": "#FFFFFF",
            "input-selection-background": "#1565C0 25%",
        },
    ),
    Theme(
        name="gruvbox-light",
        primary="#427B58",
        secondary="#B57614",
        accent="#427B58",
        warning="#AF3A03",
        error="#9D0006",
        success="#79740E",
        foreground="#3C3836",
        background="#FBF1C7",
        surface="#EBDBB2",
        panel="#D5C4A1",
        dark=False,
        variables={
            "border": "#BDAE93",
            "block-cursor-foreground": "#282828",
            "input-selection-background": "#68986640",
            "button-color-foreground": "#FBF1C7",
            "footer-background": "#EBDBB2",
            "footer-key-foreground": "#427B58",
        },
    ),
    Theme(
        name="hackerman",
        primary="#00FF21",
        secondary="#149414",
        accent="#00FF21",
        warning="#FFFF33",
        error="#FF0000",
        success="#00FF21",
        foreground="#00FF21",
        background="#0D0D0D",
        surface="#171B1F",
        panel="#1E2428",
        dark=True,
        variables={
            "border": "#0e6b0e",
            "footer-background": "#0D0D0D",
            "footer-key-foreground": "#00FF21",
            "button-color-foreground": "#0D0D0D",
            "input-selection-background": "#149414 40%",
            MODE_INSERT_COLOR_VAR: "#00FF21",
        },
    ),
    Theme(
        name="everforest",
        primary="#A7C080",
        secondary="#83C092",
        accent="#7FBBB3",
        warning="#DBBC7F",
        error="#E67E80",
        success="#A7C080",
        foreground="#D3C6AA",
        background="#232A2E",
        surface="#2D353B",
        panel="#3D484D",
        dark=True,
        variables={
            "border": "#3D484D",
            "footer-background": "#2D353B",
            "footer-key-foreground": "#A7C080",
            "button-color-foreground": "#232A2E",
            "input-selection-background": "#3D484D 40%",
        },
    ),
    Theme(
        name="kanagawa",
        primary="#7E9CD8",
        secondary="#7FB4CA",
        accent="#D27E99",
        warning="#FFA066",
        error="#E46876",
        success="#98BB6C",
        foreground="#DCD7BA",
        background="#1F1F28",
        surface="#16161D",
        panel="#223249",
        dark=True,
        variables={
            "border": "#2D4F67",
            "footer-background": "#16161D",
            "footer-key-foreground": "#7E9CD8",
            "button-color-foreground": "#1F1F28",
            "input-selection-background": "#2D4F67 40%",
        },
    ),
    Theme(
        name="matte-black",
        primary="#FFFFFF",
        secondary="#888888",
        accent="#FFFFFF",
        warning="#FFA500",
        error="#FF4444",
        success="#44FF44",
        foreground="#CCCCCC",
        background="#000000",
        surface="#0A0A0A",
        panel="#1A1A1A",
        dark=True,
        variables={
            "border": "#333333",
            "footer-background": "#0A0A0A",
            "footer-key-foreground": "#FFFFFF",
            "button-color-foreground": "#000000",
            "input-selection-background": "#333333 40%",
        },
    ),
    Theme(
        name="ristretto",
        primary="#F5D1C8",
        secondary="#C9A9A6",
        accent="#F5D1C8",
        warning="#EBD9B4",
        error="#E67E7E",
        success="#A8C4A0",
        foreground="#D5C4A1",
        background="#1C1410",
        surface="#2D2420",
        panel="#3D322C",
        dark=True,
        variables={
            "border": "#4D3C36",
            "footer-background": "#2D2420",
            "footer-key-foreground": "#F5D1C8",
            "button-color-foreground": "#1C1410",
            "input-selection-background": "#4D3C36 40%",
        },
    ),
    Theme(
        name="osaka-jade",
        primary="#7DCFB6",
        secondary="#5BA492",
        accent="#7DCFB6",
        warning="#E0C380",
        error="#D87A7A",
        success="#7DCFB6",
        foreground="#B9D7D0",
        background="#0D1512",
        surface="#1A2420",
        panel="#2D4038",
        dark=True,
        variables={
            "border": "#2D4038",
            "footer-background": "#1A2420",
            "footer-key-foreground": "#7DCFB6",
            "button-color-foreground": "#0D1512",
            "input-selection-background": "#253530 40%",
        },
    ),
    # Terminal Default - uses ANSI colors from terminal
    Theme(
        name="textual-ansi",
        primary="ansi_bright_blue",
        secondary="ansi_blue",
        accent="ansi_bright_blue",
        warning="ansi_yellow",
        error="ansi_red",
        success="ansi_green",
        foreground="ansi_default",
        background="ansi_default",
        surface="ansi_default",
        panel="ansi_default",
        dark=True,
        variables={
            "border": "ansi_bright_black",
            "border-blurred": "ansi_black",
            "button-color-foreground": "ansi_black",
            "input-cursor-background": "ansi_bright_yellow",
            "input-cursor-foreground": "ansi_black",
            "input-selection-background": "ansi_bright_blue 70%",
            "input-selection-foreground": "ansi_black",
            "scrollbar": "ansi_bright_blue",
            "scrollbar-hover": "ansi_bright_cyan",
            "scrollbar-active": "ansi_bright_blue",
            "scrollbar-background": "ansi_black",
            "scrollbar-background-hover": "ansi_black",
            "scrollbar-background-active": "ansi_black",
            "scrollbar-corner-color": "ansi_black",
        },
    ),
    # Built-in theme overrides (only adding border variable for contrast)
    Theme(
        name="dracula",
        primary="#BD93F9",
        secondary="#FF79C6",
        accent="#BD93F9",
        warning="#F1FA8C",
        error="#FF5555",
        success="#50FA7B",
        foreground="#F8F8F2",
        background="#282A36",
        surface="#2B2E3B",
        panel="#313442",
        dark=True,
        variables={
            "border": "#44475A",
            "button-color-foreground": "#282A36",
        },
    ),
    Theme(
        name="monokai",
        primary="#AE81FF",
        secondary="#66D9EF",
        accent="#AE81FF",
        warning="#E6DB74",
        error="#F92672",
        success="#A6E22E",
        foreground="#F8F8F2",
        background="#272822",
        surface="#2e2e2e",
        panel="#3E3D32",
        dark=True,
        variables={
            "border": "#3E3D32",
            "foreground-muted": "#797979",
            "input-selection-background": "#575b6190",
            "button-color-foreground": "#272822",
        },
    ),
    Theme(
        name="solarized-dark",
        primary="#268bd2",
        secondary="#2AA198",
        accent="#268bd2",
        warning="#B58900",
        error="#DC322F",
        success="#859900",
        foreground="#839496",
        background="#002b36",
        surface="#073642",
        panel="#073642",
        dark=True,
        variables={
            "border": "#586e75",
            "border-blurred": "#2f4a52",
            "button-color-foreground": "#fdf6e3",
            "footer-background": "#073642",
            "footer-key-foreground": "#fdf6e3",
            "footer-description-foreground": "#fdf6e3",
            "input-selection-background": "#073642",
        },
    ),
    Theme(
        name="tokyo-night",
        primary="#BB9AF7",
        secondary="#7AA2F7",
        accent="#BB9AF7",
        warning="#E0AF68",
        error="#F7768E",
        success="#9ECE6A",
        foreground="#C0CAF5",
        background="#1A1B26",
        surface="#24283B",
        panel="#414868",
        dark=True,
        variables={
            "border": "#414868",
            "button-color-foreground": "#24283B",
        },
    ),
    Theme(
        name="gruvbox",
        primary="#85A598",
        secondary="#FABD2F",
        accent="#85A598",
        warning="#FE8019",
        error="#FB4934",
        success="#B8BB26",
        foreground="#EBDBB2",
        background="#282828",
        surface="#3c3836",
        panel="#504945",
        dark=True,
        variables={
            "border": "#504945",
            "block-cursor-foreground": "#fbf1c7",
            "input-selection-background": "#689d6a40",
            "button-color-foreground": "#282828",
        },
    ),
    Theme(
        name="nord",
        primary="#88C0D0",
        secondary="#81A1C1",
        accent="#88C0D0",
        warning="#EBCB8B",
        error="#BF616A",
        success="#A3BE8C",
        foreground="#ECEFF4",
        background="#2E3440",
        surface="#3B4252",
        panel="#434C5E",
        dark=True,
        variables={
            "border": "#434C5E",
            "block-cursor-background": "#88C0D0",
            "block-cursor-foreground": "#2E3440",
            "block-cursor-text-style": "none",
            "footer-key-foreground": "#88C0D0",
            "input-selection-background": "#81a1c1 35%",
            "button-color-foreground": "#2E3440",
            "button-focus-text-style": "reverse",
        },
    ),
    Theme(
        name="textual-dark",
        primary="#0178D4",
        secondary="#004578",
        accent="#0178D4",
        warning="#ffa62b",
        error="#ba3c5b",
        success="#4EBF71",
        foreground="#e0e0e0",
        background="#121212",
        surface="#1e1e1e",
        panel="#2d2d2d",
        dark=True,
        variables={
            "border": "#2d2d2d",
        },
    ),
]

def _apply_mode_color_defaults(themes: list[Theme]) -> None:
    for theme in themes:
        variables = theme.variables or {}
        defaults = DEFAULT_MODE_COLORS["dark" if theme.dark else "light"]
        variables.setdefault(MODE_NORMAL_COLOR_VAR, defaults[MODE_NORMAL_COLOR_VAR])
        variables.setdefault(MODE_INSERT_COLOR_VAR, defaults[MODE_INSERT_COLOR_VAR])
        theme.variables = variables


_apply_mode_color_defaults(SQLIT_THEMES)

SQLIT_TEXTAREA_THEMES: dict[str, TextAreaTheme] = {
    "sqlit-light": TextAreaTheme(
        name="sqlit-light",
        syntax_styles={
            "keyword": Style(color="#D73A49", bold=True),
            "keyword.operator": Style(color="#D73A49"),
            "string": Style(color="#032F62"),
            "string.special": Style(color="#032F62"),
            "comment": Style(color="#6A737D", italic=True),
            "number": Style(color="#005CC5"),
            "float": Style(color="#005CC5"),
            "operator": Style(color="#D73A49"),
            "punctuation": Style(color="#24292E"),
            "punctuation.bracket": Style(color="#24292E"),
            "punctuation.delimiter": Style(color="#24292E"),
            "function": Style(color="#6F42C1"),
            "function.call": Style(color="#6F42C1"),
            "type": Style(color="#005CC5"),
            "variable": Style(color="#24292E"),
            "constant": Style(color="#005CC5"),
            "identifier": Style(color="#24292E"),
        },
    ),
    "hackerman": TextAreaTheme(
        name="hackerman",
        syntax_styles={
            "keyword": Style(color="#00FF21", bold=True),
            "keyword.operator": Style(color="#00FF21"),
            "string": Style(color="#7CFC00"),
            "string.special": Style(color="#7CFC00"),
            "comment": Style(color="#228B22", italic=True),
            "number": Style(color="#00FA9A"),
            "float": Style(color="#00FA9A"),
            "operator": Style(color="#32CD32"),
            "punctuation": Style(color="#149414"),
            "punctuation.bracket": Style(color="#149414"),
            "punctuation.delimiter": Style(color="#149414"),
            "function": Style(color="#39FF14"),
            "function.call": Style(color="#39FF14"),
            "type": Style(color="#00FF7F"),
            "variable": Style(color="#90EE90"),
            "constant": Style(color="#00FA9A"),
            "identifier": Style(color="#00FF21"),
        },
    ),
}
