--[[
    init.lua - Main Neovim configuration entry point
    
    HOW THIS WORKS:
    1. Bootstrap lazy.nvim (plugin manager)
    2. Load basic settings
    3. Load all plugins from plugins/ directory
    4. Set global keymaps and LSP keymaps
]]

-- ============================================================================
-- STEP 0: SETUP NODE PATH (for fnm)
-- ============================================================================
local fnm_current = vim.fn.trim(vim.fn.system("fnm current 2>/dev/null"))
if fnm_current ~= "" then
    local fnm_dir = vim.fn.expand("~/.local/share/fnm")
    local fnm_bin_path = fnm_dir .. "/node-versions/" .. fnm_current .. "/installation/bin"
    vim.env.PATH = fnm_bin_path .. ":" .. vim.env.PATH
end

-- ============================================================================
-- STEP 1: BOOTSTRAP LAZY.NVIM
-- ============================================================================
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not 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)

-- ============================================================================
-- STEP 2: LOAD SETTINGS
-- ============================================================================
require("basic-settings")

-- ============================================================================
-- STEP 3: LOAD PLUGINS
-- ============================================================================
require("lazy").setup("plugins")

-- ============================================================================
-- STEP 4: LSP KEYMAPS
-- ============================================================================
vim.keymap.set("n", "gd", vim.lsp.buf.definition, { desc = "Go to definition" })
vim.keymap.set("n", "gD", vim.lsp.buf.declaration, { desc = "Go to declaration" })
vim.keymap.set("n", "gi", vim.lsp.buf.implementation, { desc = "Go to implementation" })
vim.keymap.set("n", "gr", vim.lsp.buf.references, { desc = "Show references" })
vim.keymap.set("n", "K", vim.lsp.buf.hover, { desc = "Show hover docs" })
vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename, { desc = "Rename symbol" })
vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, { desc = "Code actions" })
vim.keymap.set("n", "<leader>e", vim.diagnostic.open_float, { desc = "Show diagnostic" })
-- vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, { desc = "Previous diagnostic" })
-- vim.keymap.set("n", "]d", vim.diagnostic.goto_next, { desc = "Next diagnostic" })

vim.diagnostic.config({
    virtual_text = { prefix = "●" },
    signs = true,
    float = { border = "rounded" },
})

-- ============================================================================
-- STEP 5: GLOBAL KEYMAPS
-- ============================================================================
vim.keymap.set("n", "<leader>w", ":w<CR>", { desc = "Save file" })
vim.keymap.set("n", "<leader>q", ":q<CR>", { desc = "Quit" })
vim.keymap.set("n", "<C-n>", ":Neotree filesystem reveal left<CR>", { desc = "Toggle explorer" })
vim.keymap.set("n", "<C-p>", ":Telescope find_files<CR>", { desc = "Find files" })

-- Better window navigation
vim.keymap.set("n", "<C-h>", "<C-w>h", { desc = "Navigate left" })
vim.keymap.set("n", "<C-j>", "<C-w>j", { desc = "Navigate down" })
vim.keymap.set("n", "<C-k>", "<C-w>k", { desc = "Navigate up" })
vim.keymap.set("n", "<C-l>", "<C-w>l", { desc = "Navigate right" })

-- Resize windows
vim.keymap.set("n", "<C-Up>", ":resize -2<CR>", { desc = "Resize window up" })
vim.keymap.set("n", "<C-Down>", ":resize +2<CR>", { desc = "Resize window down" })
vim.keymap.set("n", "<C-Left>", ":vertical resize -2<CR>", { desc = "Resize window left" })
vim.keymap.set("n", "<C-Right>", ":vertical resize +2<CR>", { desc = "Resize window right" })

-- Move text up/down
vim.keymap.set("n", "<A-j>", ":m .+1<CR>==", { desc = "Move line down" })
vim.keymap.set("n", "<A-k>", ":m .-2<CR>==", { desc = "Move line up" })
vim.keymap.set("v", "<A-j>", ":m '>+1<CR>gv=gv", { desc = "Move selection down" })
vim.keymap.set("v", "<A-k>", ":m '<-2<CR>gv=gv", { desc = "Move selection up" })
