local vim = vim
require("options") -- ~/.nixos/home/config/nvim/lua/options.lua
require("plugins") -- ~/.nixos/home/config/nvim/lua/plugins.lua
require("utils.t3_functions") -- ~/.nixos/home/config/nvim/lua/utils/t3_functions.lua
require("utils.t3_overrides") -- ~/.nixos/home/config/nvim/lua/utils/t3_overrides.lua
require("utils.reload") -- ~/.nixos/home/config/nvim/lua/utils/reload.lua
require("vimwiki") -- ~/.nixos/home/config/nvim/lua/vimwiki.lua
require("keymaps") -- ~/.nixos/home/config/nvim/lua/keymaps.lua
require("lsp") -- ~/.nixos/home/config/nvim/lua/lsp.lua
require("completion") -- ~/.nixos/home/config/nvim/lua/completion.lua
require("treesitter") -- ~/.nixos/home/config/nvim/lua/treesitter.lua
require("autocmds")  -- ~/.nixos/home/config/nvim/lua/autocmds.lua
require("diagnostics") -- ~/.nixos/home/config/nvim/lua/diagnostics.lua

require('tokyonight').setup({
	style = "moon", -- "storm", "moon", "day", "night"
	transparent = true, -- Enable transparent background
	terminal_colors = true, -- Enable terminal colors
	styles = {
		comments = { italic = true }, -- Italic comments
		keywords = { italic = true }, -- Italic keywords
		functions = { bold = true }, -- Bold functions
		variables = {}, -- No special style for variables
		sidebars = "dark", -- Dark sidebars
		floats = "dark", -- Dark floating windows
	},
})

local function is_linux_console()
  return vim.env.TERM == "linux"
end

local function want_truecolor()
  if is_linux_console() then return false end
  return vim.fn.has("termguicolors") == 1
			or ((vim.env.COLORTERM or ""):lower():find("truecolor") ~= nil)
end

local function set_colorscheme()
  if want_truecolor() then
    vim.o.termguicolors = true
    if not pcall(vim.cmd.colorscheme, "tokyonight") then
      pcall(vim.cmd.colorscheme, "habamax")
    end
  else
    vim.o.termguicolors = false
    if not pcall(vim.cmd.colorscheme, "industry") then
      pcall(vim.cmd.colorscheme, "default")
    end
  end
end

vim.api.nvim_create_autocmd("VimEnter", {
  callback = function()
    set_colorscheme()

    local tty = is_linux_console()

    -- LUALINE: no icons, simple separators, TTY theme
    pcall(function()
      local ok, lualine = pcall(require, "lualine")
      if not ok then return end
      if tty then
        lualine.setup({
          options = {
            theme = "16color",          -- built-in, safe for TTY
            icons_enabled = false,
            component_separators = "",
            section_separators = "",
            globalstatus = true,
          },
          sections = {
            lualine_a = { "mode" },
            lualine_b = { "branch" },
            lualine_c = { { "filename", file_status = true, path = 1 } },
            lualine_x = { "encoding", "fileformat", "filetype" },
            lualine_y = { "progress" },
            lualine_z = { "location" },
          },
        })
      else
        lualine.setup({
          options = {
            theme = "tokyonight",
            icons_enabled = true,
            globalstatus = true,
          },
        })
      end
    end)

    -- BARBAR: kill icons in TTY, simplify separators
    pcall(function()
      vim.g.barbar_auto_setup = false
      local ok, barbar = pcall(require, "barbar")
      if not ok then return end
      if tty then
        barbar.setup({
          animation = false,
          auto_hide = false,
          sidebar_filetypes = { NvimTree = true, undotree = { text = "Undotree" } },
          icons = {
            -- aggressively disable anything icon-like
            buffer_index = false,
            buffer_number = false,
            button = "",                 -- close button
            diagnostics = {
              [vim.diagnostic.severity.ERROR] = { enabled = false },
              [vim.diagnostic.severity.WARN]  = { enabled = false },
              [vim.diagnostic.severity.INFO]  = { enabled = false },
              [vim.diagnostic.severity.HINT]  = { enabled = false },
            },
            gitsigns = { added = {enabled=false}, changed = {enabled=false}, deleted = {enabled=false} },
            filetype = { enabled = false },
            pinned = { button = "", filename = true },
            modified = { button = "*" }, -- simple ASCII marker
            separator = { left = "|", right = "|" },
            inactive = { separator = { left = "", right = "" } },
          },
        })
      else
        barbar.setup({}) -- your normal config, or keep defaults
      end
    end)

    -- If you use web-devicons, disable it in TTY to avoid glyphs
    if is_linux_console() then
      -- If using lazy.nvim, better to conditionally load the plugin (see spec below).
      if package.loaded["nvim-web-devicons"] then
        local ok, devicons = pcall(require, "nvim-web-devicons")
        if ok and devicons.has_loaded() then
          -- there's no runtime "disable", but not using it in barbar/lualine is enough.
        end
      end
    end
  end,
})

