-- ~/.config/wezterm/wezterm.lua
local wezterm = require 'wezterm'
local act = wezterm.action

-- Create a config object
local config = wezterm.config_builder()

-- local statusbar = wezterm.plugin.require 'https://github.com/Townk/statusbar.wezterm'

-- ╭──────────────────────────────────────────────────────────╮
-- │ Shell Configuration                                      │
-- ╰──────────────────────────────────────────────────────────╯
config.default_prog = { '/bin/zsh' }

-- ╭──────────────────────────────────────────────────────────╮
-- │ Appearance                                               │
-- ╰──────────────────────────────────────────────────────────╯
-- Font configuration
config.font = wezterm.font_with_fallback({
  { family = 'FiraCode Nerd Font',     weight = 'Regular' },
  { family = 'JetBrains Mono',         weight = 'Regular' },
  { family = 'Symbols Nerd Font Mono', scale = 0.85 },
})
config.font_size = 10.0
config.harfbuzz_features = { "calt", "clig", "liga", "kern", "dlig" }
config.warn_about_missing_glyphs = false

-- Base Color scheme
config.color_scheme = 'Dracula' -- Base scheme, specific colors below will override where needed

-- Window Appearance
config.window_background_opacity = 0.7
config.window_decorations = "INTEGRATED_BUTTONS|RESIZE"
config.window_padding = {
  left = '1cell',
  right = '1cell',
  top = '0.5cell',
  bottom = '0.5cell',
}

-- Cursor appearance
config.default_cursor_style = "SteadyBlock"
config.cursor_blink_rate = 0 -- Steady cursor (no blinking)

-- Inactive Pane Dimming
config.inactive_pane_hsb = {
  saturation = 0.9,
  brightness = 0.8,
}

-- ╭──────────────────────────────────────────────────────────╮
-- │ Tab Bar Styling                                          │
-- ╰──────────────────────────────────────────────────────────╯
config.hide_tab_bar_if_only_one_tab = false
config.tab_bar_at_bottom = false
config.use_fancy_tab_bar = true -- Keep this false for custom styling below
config.show_tab_index_in_tab_bar = true
config.tab_max_width = 25

-- Detailed Color Overrides for UI Elements (including Tab Bar)
config.colors = {
  -- Base Dracula colors (can be inherited from the scheme, but explicit is fine)
  foreground = "#f8f8f2", -- WHITE
  background = "#000000", -- SCHWARZ
  cursor_bg = "#ec5353",
  cursor_fg = "#282a36",
  cursor_border = "#f8f8f2",
  selection_bg = "#44475a",
  selection_fg = "#f8f8f2",
  -- ANSI Colors (ensure they match Dracula if needed)
  ansi = {
    '#000000', '#ff5555', '#50fa7b', '#f1fa8c',
    '#bd93f9', '#ff79c6', '#8be9fd', '#bfbfbf',
  },
  brights = {
    '#4d4d4d', '#ff6e6e', '#69ff94', '#ffffa5',
    '#d6acff', '#ff92df', '#a4ffff', '#ffffff',
  },

  -- Tab Bar Colors
  tab_bar = {
    background = "#000000", -- Slightly darker background for the bar itself
    active_tab = {
      bg_color = "#00a6e9", -- Blau
      fg_color = "#282a36", -- Dark background for text
      intensity = "Bold",   -- Make active tab text bold
      underline = "None",
      italic = false,
      strikethrough = false,
    },
    inactive_tab = {
      bg_color = "#44475a", -- Dracula selection color (muted)
      fg_color = "#f8f8f2", -- Standard foreground
    },
    inactive_tab_hover = {
      bg_color = "#6272a4", -- Dracula comment color (brighter on hover)
      fg_color = "#f8f8f2",
      italic = true,        -- Italicize on hover
    },
    new_tab = {
      bg_color = "#282a36", -- Standard background
      fg_color = "#f8f8f2", -- Standard foreground
    },
    new_tab_hover = {
      bg_color = "#ff79c6", -- Dracula pink
      fg_color = "#282a36", -- Dark text
    },
  },

  -- Optional: Subtle pane borders using Dracula comment color
  split = "#6272a4",
}

-- Window Frame / Title Bar Styling (Subtle adjustments)
config.window_frame = {
  font = wezterm.font { family = 'FiraCode Nerd Font', weight = 'Bold' }, -- Use bold font for title/tabs
  font_size = config.font_size,                                           -- Match terminal font size
  active_titlebar_bg = "#282a36",                                         -- Keep consistent with background
  inactive_titlebar_bg = "#21222c",                                       -- Slightly darker when inactive
  -- Border colors removed here, using 'split' color above for pane lines instead
}

-- ╭──────────────────────────────────────────────────────────╮
-- │ Scrollback & Performance                                 │
-- ╰──────────────────────────────────────────────────────────╯
config.scrollback_lines = 10000
config.enable_scroll_bar = true -- Keep the scrollbar visible
config.term = "wezterm"
config.front_end = "WebGpu"
config.webgpu_power_preference = "HighPerformance"
config.animation_fps = 60
config.max_fps = 165

-- ╭──────────────────────────────────────────────────────────╮
-- │ Keybindings                                              │
-- ╰──────────────────────────────────────────────────────────╯
config.leader = { key = "Space", mods = "CTRL" }

config.keys = {
  -- { key = "m", mods = "LEADER", action = act.ShowMenu },

  { key = "t",   mods = "LEADER",       action = act.SpawnTab("CurrentPaneDomain") },
  { key = "w",   mods = "LEADER",       action = act.CloseCurrentTab({ confirm = true }) },
  { key = "Tab", mods = "LEADER",       action = act.ActivateTabRelative(1) },
  { key = "Tab", mods = "LEADER|SHIFT", action = act.ActivateTabRelative(-1) },

  { key = "b",   mods = "LEADER",       action = act.SplitVertical({ domain = "CurrentPaneDomain" }) },
  { key = "h",   mods = "LEADER",       action = act.SplitHorizontal({ domain = "CurrentPaneDomain" }) },

  { key = "h",   mods = "LEADER",       action = act.ActivatePaneDirection("Left") },
  { key = "j",   mods = "LEADER",       action = act.ActivatePaneDirection("Down") },
  { key = "k",   mods = "LEADER",       action = act.ActivatePaneDirection("Up") },
  { key = "l",   mods = "LEADER",       action = act.ActivatePaneDirection("Right") },


  { key = "+",   mods = "LEADER|SHIFT", action = act.IncreaseFontSize },
  { key = "-",   mods = "LEADER|SHIFT", action = act.DecreaseFontSize },


  {
    key = ",",
    mods = "LEADER",
    action = act.SpawnCommandInNewTab {
      args = { "xdg-open", wezterm.config_dir .. "/wezterm.lua" },
    }
  },
}

-- ╭──────────────────────────────────────────────────────────╮
-- │ Mouse Configuration (Disabled)                           │
-- ╰──────────────────────────────────────────────────────────╯
-- config.mouse_bindings = {} -- Explicitly empty or commented out

-- ╭──────────────────────────────────────────────────────────╮
-- │ Advanced Features & Event Handlers                       │
-- ╰──────────────────────────────────────────────────────────╯

-- Automatically reload configuration notification (Optional, uncomment if desired)
-- wezterm.on("window-config-reloaded", function(window, pane)
--   window:toast_notification("WezTerm", "Konfiguration neu geladen!", nil, 3000)
-- end)

-- Custom tab title formatting (Enhanced version)
wezterm.on("format-tab-title", function(tab, tabs, panes, config, hover, max_width)
  local pane = tab.active_pane
  local process_name = pane.foreground_process_name or ""
  process_name = process_name:gsub(".*/", "") -- Extract basename

  -- Shorten common shell names or identify key processes
  local display_name = process_name:upper() -- Default to uppercase name
  if process_name == "zsh" then
    display_name = "ZSH"
  elseif process_name == "bash" then
    display_name = "BASH"
  elseif process_name == "fish" then
    display_name = "FISH"
  elseif process_name == "nvim" or process_name == "vim" then
    display_name = "NVIM" -- Or VIM
  elseif process_name == "htop" then
    display_name = "HTOP"
  elseif process_name == "ssh" then
    display_name = "SSH"
    -- Add more specific names if needed
  end

  local index = ""
  if config.show_tab_index_in_tab_bar then
    index = tostring(tab.tab_index + 1) .. ":"
  end

  -- Add indicator for active tab (using Nerd Font icons if available)
  local prefix = " " -- Default space
  if tab.is_active then
    prefix = "󰓃 " -- Example: Active dot icon
  else
    prefix = "󰓃 " -- Example: Inactive dot icon (or just space " ")
  end

  local title = prefix .. index .. " " .. display_name .. " "

  -- Truncate if necessary
  local title_width = wezterm.nerdfonts.string_width(title) -- Use nerdfonts for accurate width
  if max_width and title_width > max_width then
    title = wezterm.truncate_right(title, max_width)
  end

  return title
end)



-- Remove the status bar related code entirely
-- config.status_update_interval = 1000 -- REMOVED
-- local function make_component(...) end -- REMOVED
-- local separator_fg = ... -- REMOVED
-- local separator_char = ... -- REMOVED
-- wezterm.on("update-status", function(window, pane) ... end) -- REMOVED

-- Apply the configuration
return config
