---@diagnostic disable: undefined-global
local M = {}

function M.setup()
  vim.cmd("highlight clear")
  if vim.fn.exists("syntax_on") == 1 then
    vim.cmd("syntax reset")
  end

  vim.o.termguicolors = true
  vim.g.colors_name = "Serpensortia"

  local c = {
    bg        = "#080c08",
    surface   = "#131d13",
    surface2  = "#424a42",
    fg        = "#c0c0c0",
    muted     = "#424a42",
    border    = "#0b110b",
    accent    = "#1c7a44",
    warn      = "#dc143c",

    -- richer editor palette inspired by Islands Dark / Darcula-like schemes
    green     = "#1c7a44",
    red       = "#dc143c",
    blue      = "#6ca0ff",
    cyan      = "#67c7ff",
    yellow    = "#d9c36a",
    orange    = "#d78a4a",
    purple    = "#b48ef7",
    teal      = "#4db6ac",

    subtle    = "#989898",
    comment   = "#424a42",
    add       = "#0c2415",
    change    = "#292c29",
    delete    = "#320e12",
  }

  local function hi(group, opts)
    vim.api.nvim_set_hl(0, group, opts)
  end

  -- Base UI
  hi("Normal",           { fg = c.fg, bg = c.bg })
  hi("NormalNC",         { fg = c.fg, bg = c.bg })
  hi("NormalFloat",      { fg = c.fg, bg = c.surface })
  hi("FloatBorder",      { fg = c.border, bg = c.surface })
  hi("FloatTitle",       { fg = c.green, bg = c.surface, bold = true })
  hi("CursorLine",       { bg = c.surface })
  hi("CursorColumn",     { bg = c.surface })
  hi("ColorColumn",      { bg = c.surface })
  hi("Conceal",          { fg = c.muted })
  hi("LineNr",           { fg = c.muted, bg = c.bg })
  hi("CursorLineNr",     { fg = c.green, bg = c.bg, bold = true })
  hi("SignColumn",       { fg = c.fg, bg = c.bg })
  hi("EndOfBuffer",      { fg = c.bg, bg = c.bg })
  hi("VertSplit",        { fg = c.border, bg = c.bg })
  hi("WinSeparator",     { fg = c.border, bg = c.bg })
  hi("Folded",           { fg = c.muted, bg = c.surface })
  hi("FoldColumn",       { fg = c.muted, bg = c.bg })
  hi("MatchParen",       { fg = c.yellow, bg = c.surface2, bold = true })
  hi("Whitespace",       { fg = c.border })
  hi("NonText",          { fg = c.border })

  -- Selection / search
  hi("Visual",           { bg = c.surface2 })
  hi("VisualNOS",        { bg = c.surface2 })
  hi("Search",           { fg = c.bg, bg = c.yellow, bold = true })
  hi("IncSearch",        { fg = c.bg, bg = c.orange, bold = true })
  hi("CurSearch",        { fg = c.bg, bg = c.orange, bold = true })
  hi("Substitute",       { fg = c.bg, bg = c.green, bold = true })

  -- Popup menu
  hi("Pmenu",            { fg = c.fg, bg = c.surface })
  hi("PmenuSel",         { fg = c.bg, bg = c.green, bold = true })
  hi("PmenuKind",        { fg = c.cyan, bg = c.surface })
  hi("PmenuKindSel",     { fg = c.bg, bg = c.green, bold = true })
  hi("PmenuExtra",       { fg = c.muted, bg = c.surface })
  hi("PmenuExtraSel",    { fg = c.bg, bg = c.green })
  hi("PmenuSbar",        { bg = c.surface2 })
  hi("PmenuThumb",       { bg = c.border })

  -- Tabs / status
  hi("StatusLine",       { fg = c.fg, bg = c.surface })
  hi("StatusLineNC",     { fg = c.muted, bg = c.surface })
  hi("TabLine",          { fg = c.muted, bg = c.surface })
  hi("TabLineSel",       { fg = c.fg, bg = c.bg, bold = true })
  hi("TabLineFill",      { bg = c.surface })
  hi("WinBar",           { fg = c.fg, bg = c.bg })
  hi("WinBarNC",         { fg = c.muted, bg = c.bg })

  -- Syntax
  hi("Comment",          { fg = c.comment, italic = true })
  hi("Constant",         { fg = c.yellow, bold = true })
  hi("String",           { fg = c.orange })
  hi("Character",        { fg = c.orange })
  hi("Number",           { fg = c.yellow, bold = true })
  hi("Boolean",          { fg = c.purple, bold = true })
  hi("Float",            { fg = c.yellow })
  hi("Identifier",       { fg = c.fg })
  hi("Function",         { fg = c.green, bold = true })
  hi("Statement",        { fg = c.purple, bold = true })
  hi("Conditional",      { fg = c.purple, bold = true })
  hi("Repeat",           { fg = c.purple, bold = true })
  hi("Label",            { fg = c.orange })
  hi("Operator",         { fg = c.subtle })
  hi("Keyword",          { fg = c.purple, italic = true, bold = true })
  hi("Exception",        { fg = c.red, bold = true })
  hi("PreProc",          { fg = c.blue })
  hi("Include",          { fg = c.blue, italic = true })
  hi("Define",           { fg = c.purple })
  hi("Macro",            { fg = c.purple, bold = true })
  hi("PreCondit",        { fg = c.blue })
  hi("Type",             { fg = c.cyan, bold = true })
  hi("Typedef",          { fg = c.cyan, bold = true })
  hi("StorageClass",     { fg = c.blue, italic = true })
  hi("Structure",        { fg = c.cyan, bold = true })
  hi("Special",          { fg = c.teal })
  hi("SpecialChar",      { fg = c.orange })
  hi("Tag",              { fg = c.green })
  hi("Delimiter",        { fg = c.subtle })
  hi("SpecialComment",   { fg = c.comment, italic = true })
  hi("Debug",            { fg = c.red, bold = true })
  hi("Title",            { fg = c.green, bold = true })
  hi("Todo",             { fg = c.bg, bg = c.yellow, bold = true })

  -- Diagnostics
  hi("DiagnosticError",            { fg = c.red })
  hi("DiagnosticWarn",             { fg = c.orange })
  hi("DiagnosticInfo",             { fg = c.blue })
  hi("DiagnosticHint",             { fg = c.teal })
  hi("DiagnosticOk",               { fg = c.green })

  hi("DiagnosticVirtualTextError", { fg = c.red, bg = c.delete })
  hi("DiagnosticVirtualTextWarn",  { fg = c.orange, bg = c.surface })
  hi("DiagnosticVirtualTextInfo",  { fg = c.blue, bg = c.surface })
  hi("DiagnosticVirtualTextHint",  { fg = c.teal, bg = c.surface })

  hi("DiagnosticUnderlineError",   { undercurl = true, sp = c.red })
  hi("DiagnosticUnderlineWarn",    { undercurl = true, sp = c.orange })
  hi("DiagnosticUnderlineInfo",    { undercurl = true, sp = c.blue })
  hi("DiagnosticUnderlineHint",    { undercurl = true, sp = c.teal })

  hi("DiagnosticSignError",        { fg = c.red, bg = c.bg })
  hi("DiagnosticSignWarn",         { fg = c.orange, bg = c.bg })
  hi("DiagnosticSignInfo",         { fg = c.blue, bg = c.bg })
  hi("DiagnosticSignHint",         { fg = c.teal, bg = c.bg })

  -- LSP references / semantic-ish
  hi("LspReferenceText",           { bg = c.surface2 })
  hi("LspReferenceRead",           { bg = c.surface2 })
  hi("LspReferenceWrite",          { bg = c.surface2, bold = true })
  hi("LspInlayHint",               { fg = c.muted, bg = c.surface, italic = true })
  hi("LspCodeLens",                { fg = c.muted, italic = true })
  hi("LspCodeLensSeparator",       { fg = c.border })

  -- Diff / Git
  hi("DiffAdd",                    { bg = c.add, fg = c.green })
  hi("DiffChange",                 { bg = c.change, fg = c.blue })
  hi("DiffDelete",                 { bg = c.delete, fg = c.red })
  hi("DiffText",                   { bg = c.blue, fg = c.bg, bold = true })

  hi("Added",                      { fg = c.green })
  hi("Changed",                    { fg = c.blue })
  hi("Removed",                    { fg = c.red })

  hi("GitSignsAdd",                { fg = c.green, bg = c.bg })
  hi("GitSignsChange",             { fg = c.blue, bg = c.bg })
  hi("GitSignsDelete",             { fg = c.red, bg = c.bg })
  hi("GitSignsCurrentLineBlame",   { fg = c.muted, italic = true })

  -- Treesitter
  hi("@comment",                   { link = "Comment" })
  hi("@comment.documentation",     { fg = c.comment, italic = true })

  hi("@string",                    { link = "String" })
  hi("@string.escape",             { fg = c.yellow, bold = true })
  hi("@string.regex",              { fg = c.orange })
  hi("@string.special",            { fg = c.orange })

  hi("@character",                 { link = "Character" })
  hi("@character.special",         { fg = c.orange })

  hi("@number",                    { link = "Number" })
  hi("@number.float",              { link = "Float" })
  hi("@boolean",                   { link = "Boolean" })
  hi("@constant",                  { link = "Constant" })
  hi("@constant.builtin",          { fg = c.yellow, italic = true })
  hi("@constant.macro",            { fg = c.purple, bold = true })

  hi("@variable",                  { fg = c.fg })
  hi("@variable.builtin",          { fg = c.red, italic = true })
  hi("@variable.parameter",        { fg = c.fg, italic = true })
  hi("@variable.member",           { fg = c.cyan })

  hi("@module",                    { fg = c.blue })
  hi("@module.builtin",            { fg = c.blue, italic = true })
  hi("@label",                     { fg = c.orange })

  hi("@function",                  { link = "Function" })
  hi("@function.builtin",          { fg = c.blue, italic = true, bold = true })
  hi("@function.call",             { fg = c.green })
  hi("@function.macro",            { fg = c.purple, bold = true })
  hi("@method",                    { fg = c.green })
  hi("@method.call",               { fg = c.green })

  hi("@constructor",               { fg = c.cyan, bold = true })
  hi("@operator",                  { link = "Operator" })

  hi("@keyword",                   { link = "Keyword" })
  hi("@keyword.function",          { fg = c.purple, bold = true })
  hi("@keyword.operator",          { fg = c.purple, italic = true })
  hi("@keyword.return",            { fg = c.purple, bold = true, italic = true })
  hi("@keyword.import",            { fg = c.blue, italic = true })
  hi("@keyword.conditional",       { fg = c.purple, bold = true })
  hi("@keyword.repeat",            { fg = c.purple, bold = true })
  hi("@keyword.exception",         { fg = c.red, bold = true })

  hi("@type",                      { link = "Type" })
  hi("@type.builtin",              { fg = c.blue, italic = true, bold = true })
  hi("@type.definition",           { fg = c.cyan, bold = true })
  hi("@attribute",                 { fg = c.yellow })
  hi("@property",                  { fg = c.cyan })
  hi("@field",                     { fg = c.cyan })
  hi("@parameter",                 { fg = c.fg, italic = true })

  hi("@punctuation.delimiter",     { fg = c.subtle })
  hi("@punctuation.bracket",       { fg = c.subtle })
  hi("@punctuation.special",       { fg = c.orange })

  hi("@tag",                       { fg = c.green, bold = true })
  hi("@tag.attribute",             { fg = c.yellow })
  hi("@tag.delimiter",             { fg = c.subtle })

  -- Telescope
  hi("TelescopeNormal",            { fg = c.fg, bg = c.surface })
  hi("TelescopeBorder",            { fg = c.border, bg = c.surface })
  hi("TelescopeTitle",             { fg = c.green, bg = c.surface, bold = true })
  hi("TelescopePromptNormal",      { fg = c.fg, bg = c.surface })
  hi("TelescopePromptBorder",      { fg = c.border, bg = c.surface })
  hi("TelescopePromptTitle",       { fg = c.bg, bg = c.green, bold = true })
  hi("TelescopePromptPrefix",      { fg = c.green, bg = c.surface })
  hi("TelescopeResultsNormal",     { fg = c.fg, bg = c.surface })
  hi("TelescopeResultsBorder",     { fg = c.border, bg = c.surface })
  hi("TelescopePreviewNormal",     { fg = c.fg, bg = c.surface })
  hi("TelescopePreviewBorder",     { fg = c.border, bg = c.surface })
  hi("TelescopeSelection",         { fg = c.fg, bg = c.surface2 })
  hi("TelescopeSelectionCaret",    { fg = c.green, bg = c.surface2 })
  hi("TelescopeMatching",          { fg = c.yellow, bold = true })

  -- nvim-cmp
  hi("CmpDocumentation",           { fg = c.fg, bg = c.surface })
  hi("CmpDocumentationBorder",     { fg = c.border, bg = c.surface })
  hi("CmpItemAbbr",                { fg = c.fg })
  hi("CmpItemAbbrMatch",           { fg = c.green, bold = true })
  hi("CmpItemAbbrMatchFuzzy",      { fg = c.green, italic = true })
  hi("CmpItemAbbrDeprecated",      { fg = c.muted, strikethrough = true })
  hi("CmpItemKind",                { fg = c.cyan })
  hi("CmpItemMenu",                { fg = c.muted })

  hi("CmpItemKindFunction",        { fg = c.green })
  hi("CmpItemKindMethod",          { fg = c.green })
  hi("CmpItemKindConstructor",     { fg = c.cyan })
  hi("CmpItemKindVariable",        { fg = c.fg })
  hi("CmpItemKindField",           { fg = c.cyan })
  hi("CmpItemKindProperty",        { fg = c.cyan })
  hi("CmpItemKindClass",           { fg = c.cyan })
  hi("CmpItemKindInterface",       { fg = c.blue })
  hi("CmpItemKindStruct",          { fg = c.cyan })
  hi("CmpItemKindModule",          { fg = c.blue })
  hi("CmpItemKindKeyword",         { fg = c.purple })
  hi("CmpItemKindText",            { fg = c.fg })
  hi("CmpItemKindSnippet",         { fg = c.orange })
  hi("CmpItemKindFile",            { fg = c.fg })
  hi("CmpItemKindFolder",          { fg = c.green })
  hi("CmpItemKindEnum",            { fg = c.blue })
  hi("CmpItemKindEnumMember",      { fg = c.yellow })
  hi("CmpItemKindConstant",        { fg = c.yellow })
  hi("CmpItemKindValue",           { fg = c.yellow })
  hi("CmpItemKindUnit",            { fg = c.orange })

  -- Neo-tree / NvimTree
  hi("Directory",                  { fg = c.green, bold = true })
  hi("NeoTreeNormal",              { fg = c.fg, bg = c.bg })
  hi("NeoTreeNormalNC",            { fg = c.fg, bg = c.bg })
  hi("NeoTreeDirectoryName",       { fg = c.green, bold = true })
  hi("NeoTreeDirectoryIcon",       { fg = c.green })
  hi("NeoTreeFileName",            { fg = c.fg })
  hi("NeoTreeFileIcon",            { fg = c.subtle })
  hi("NeoTreeIndentMarker",        { fg = c.border })
  hi("NeoTreeRootName",            { fg = c.cyan, bold = true })
  hi("NeoTreeGitAdded",            { fg = c.green })
  hi("NeoTreeGitModified",         { fg = c.blue })
  hi("NeoTreeGitDeleted",          { fg = c.red })
  hi("NeoTreeGitConflict",         { fg = c.orange, bold = true })
  hi("NeoTreeFloatBorder",         { fg = c.border, bg = c.surface })
  hi("NeoTreeFloatTitle",          { fg = c.green, bg = c.surface, bold = true })
  hi("NvimTreeNormal",             { fg = c.fg, bg = c.bg })
  hi("NvimTreeFolderName",         { fg = c.green, bold = true })
  hi("NvimTreeRootFolder",         { fg = c.cyan, bold = true })
  hi("NvimTreeOpenedFolderName",   { fg = c.green, bold = true })
  hi("NvimTreeIndentMarker",       { fg = c.border })
  hi("NvimTreeGitDirty",           { fg = c.blue })
  hi("NvimTreeGitNew",             { fg = c.green })
  hi("NvimTreeGitDeleted",         { fg = c.red })

  -- which-key
  hi("WhichKey",                   { fg = c.green, bold = true })
  hi("WhichKeyGroup",              { fg = c.cyan })
  hi("WhichKeyDesc",               { fg = c.fg })
  hi("WhichKeySeparator",          { fg = c.border })
  hi("WhichKeyFloat",              { bg = c.surface })
  hi("WhichKeyBorder",             { fg = c.border, bg = c.surface })
  hi("WhichKeyValue",              { fg = c.muted })

  -- Indent guides
  hi("IblIndent",                  { fg = c.border })
  hi("IblScope",                   { fg = c.blue })
  hi("IndentBlanklineChar",        { fg = c.border })
  hi("IndentBlanklineContextChar", { fg = c.blue })

  -- lazy.nvim
  hi("LazyNormal",                 { fg = c.fg, bg = c.surface })
  hi("LazyBorder",                 { fg = c.border, bg = c.surface })
  hi("LazyButton",                 { fg = c.fg, bg = c.surface2 })
  hi("LazyButtonActive",           { fg = c.bg, bg = c.green, bold = true })
  hi("LazyH1",                     { fg = c.bg, bg = c.green, bold = true })
  hi("LazySpecial",                { fg = c.purple })
  hi("LazyReasonPlugin",           { fg = c.cyan })
  hi("LazyReasonEvent",            { fg = c.yellow })
  hi("LazyReasonKeys",             { fg = c.green })
  hi("LazyReasonCmd",              { fg = c.orange })
  hi("LazyReasonStart",            { fg = c.green })

  -- Markdown / help
  hi("markdownHeadingDelimiter",   { fg = c.green, bold = true })
  hi("markdownCode",               { fg = c.orange })
  hi("markdownCodeBlock",          { fg = c.orange })
  hi("markdownLinkText",           { fg = c.cyan, underline = true })
  hi("markdownUrl",                { fg = c.blue, underline = true })
  hi("helpCommand",                { fg = c.green })
  hi("helpExample",                { fg = c.orange })

  -- Notifications / misc
  hi("ErrorMsg",                   { fg = c.red, bold = true })
  hi("WarningMsg",                 { fg = c.orange })
  hi("MoreMsg",                    { fg = c.green })
  hi("ModeMsg",                    { fg = c.cyan, bold = true })
  hi("Question",                   { fg = c.green, bold = true })
end

return M
