-- ── Bootstrap lazy.nvim ───────────────────────────────────────────────────────
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
    vim.fn.system({
        "git", "clone", "--filter=blob:none",
        "https://github.com/folke/lazy.nvim.git",
        "--branch=stable", lazypath,
    })
end
vim.opt.rtp:prepend(lazypath)

-- ── Plugins ───────────────────────────────────────────────────────────────────
require("lazy").setup({
    { dir = vim.fn.expand("~/Dotfiles/nvim/theme/cyberqueer.nvim") },
    "rktjmp/lush.nvim",
    "tpope/vim-sensible",
    "junegunn/goyo.vim",
    "arecarn/vim-crunch",
    "preservim/nerdtree",
    "ryanoasis/vim-devicons",
    { "junegunn/fzf", build = function() vim.fn["fzf#install"]() end },
    "junegunn/fzf.vim",
    "vim-airline/vim-airline",
    "vim-airline/vim-airline-themes",
    "voldikss/vim-floaterm",
    "rust-lang/rust.vim",
    "norcalli/nvim-colorizer.lua",
    { "neoclide/coc.nvim", branch = "release" },
    { "mg979/vim-visual-multi", branch = "master" },
    "SirVer/ultisnips",
    "honza/vim-snippets",
    "mfussenegger/nvim-dap",
    "elihunter173/dirbuf.nvim",
    "tpope/vim-dadbod",
    "kristijanhusak/vim-dadbod-ui",
    "kristijanhusak/vim-dadbod-completion",
    "nvim-mini/mini.icons",
    "tadmccorkle/markdown.nvim",
    { "ellisonleao/glow.nvim", config = true },
    {
        "greggh/claude-code.nvim",
        dependencies = { "nvim-lua/plenary.nvim" },
        config = function()
            require("claude-code").setup()
        end,
    },
}, {
    -- keep lazy's own UI out of the way on first install
    install = { colorscheme = { "habamax" } },
})

-- ── Colorscheme & UI ──────────────────────────────────────────────────────────
vim.cmd("colorscheme cyberqueer")

vim.g.airline_powerline_fonts = 1
vim.g.airline_theme           = "cyberqueer"

local ipaddr   = vim.trim(vim.fn.system("hostname -i"))
local hostname = vim.trim(vim.fn.system("hostname -s"))
vim.g.airline_section_x = "IP:" .. ipaddr .. " DNS:" .. hostname

-- ── Providers ─────────────────────────────────────────────────────────────────
vim.g.loaded_ruby_provider = 0
vim.g.loaded_perl_provider = 0

-- ── Editor options ────────────────────────────────────────────────────────────
vim.cmd("filetype plugin indent on")
vim.cmd("syntax enable")

vim.opt.number         = true
vim.opt.relativenumber = true
vim.opt.cursorline     = true
vim.opt.cursorcolumn   = true
vim.opt.showmode       = false
vim.opt.shiftwidth     = 4
vim.opt.scrolloff      = 5
vim.opt.wrap           = false
vim.opt.incsearch      = true
vim.opt.ignorecase     = true
vim.opt.smartcase      = true
vim.opt.showcmd        = true
vim.opt.showmatch      = true
vim.opt.hlsearch       = true
vim.opt.history        = 1000
vim.opt.wildmenu       = true
vim.opt.wildmode       = "list:longest"

-- ── Keymaps ───────────────────────────────────────────────────────────────────
-- window navigation
vim.keymap.set("n", "<C-l>", "<C-w>w")
vim.keymap.set("n", "<C-h>", "<C-w>h")
vim.keymap.set("n", "<C-j>", "<C-w>j")
vim.keymap.set("n", "<C-k>", "<C-w>k")

-- quick actions
vim.keymap.set("n", "t", ":FloatermNew<CR>",    { silent = true })
vim.keymap.set("n", "e", ":NERDTreeToggle<CR><C-W>l", { silent = true })
vim.keymap.set("n", "s", ":DBUIToggle<CR>",     { silent = true })
vim.keymap.set("n", "x", "<Plug>(DBUI_ExecuteQuery)", { remap = true })
vim.keymap.set("n", "q", function()
  local ok = pcall(vim.cmd, "wq")
  if not ok then vim.cmd("q") end
end, { silent = true })

-- insert mode completion
vim.keymap.set("i", "<TAB>",   "<C-N>")
vim.keymap.set("i", "<S-TAB>", "<TAB>")

-- sudo save
vim.cmd("ca w!! w !sudo tee '%'")

-- visual block shorthand
vim.cmd("command! Vb normal! <C-v>")

-- ── UltiSnips ─────────────────────────────────────────────────────────────────
vim.g.UltiSnipsExpandTrigger      = "<C-tab>"
vim.g.UltiSnipsJumpForwardTrigger = "<c-b>"
vim.g.UltiSnipsJumpBackwardTrigger = "<c-z>"
vim.g.UltiSnipsEditSplit           = "vertical"

-- ── CoC ───────────────────────────────────────────────────────────────────────
vim.g.coc_global_extensions = {
    "coc-snippets", "coc-powershell", "coc-sh", "coc-omnisharp",
    "coc-clangd", "coc-json", "coc-css", "coc-git", "coc-pyright", "coc-sql",
}

vim.g.coc_snippet_next = "<c-j>"
vim.g.coc_snippet_prev = "<c-k>"

vim.keymap.set("i", "<C-l>", "<Plug>(coc-snippets-expand)",      { remap = true })
vim.keymap.set("v", "<C-j>", "<Plug>(coc-snippets-select)",      { remap = true })
vim.keymap.set("i", "<C-j>", "<Plug>(coc-snippets-expand-jump)", { remap = true })
vim.keymap.set("x", "<leader>x", "<Plug>(coc-convert-snippet)",  { remap = true })

-- tab/s-tab navigate CoC pum, else fall through
vim.keymap.set("i", "<Tab>", function()
    return vim.fn["coc#pum#visible"]() == 1 and vim.fn["coc#pum#next"](1) or "<Tab>"
end, { expr = true, silent = true })

vim.keymap.set("i", "<S-Tab>", function()
    return vim.fn["coc#pum#visible"]() == 1 and vim.fn["coc#pum#prev"](1) or "<S-Tab>"
end, { expr = true, silent = true })

-- CR confirms CoC selection
vim.keymap.set("i", "<CR>", function()
    return vim.fn["coc#pum#visible"]() == 1 and vim.fn["coc#pum#confirm"]() or "<CR>"
end, { expr = true, silent = true })
