-- LSP Configuration
local lspconfig = require('lspconfig')

-- Setup Mason (already done via lazy.nvim, but ensure it's initialized)
require('mason').setup()

-- Setup LSP capabilities for nvim-cmp
local capabilities = require('cmp_nvim_lsp').default_capabilities()

-- Function to attach LSP to buffers
local on_attach = function(client, bufnr)
    -- Enable completion triggered by <c-x><c-o>
    vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')

    -- Mappings for LSP actions
    local bufopts = { noremap = true, silent = true, buffer = bufnr }
    vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts)
    vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts)
    vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts)
    vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts)
    vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, bufopts)
    vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, bufopts)
    vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, bufopts)
    vim.keymap.set('n', '<space>wl', function()
        print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
    end, bufopts)
    vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, bufopts)
    vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, bufopts)
    vim.keymap.set('n', '<space>ca', vim.lsp.buf.code_action, bufopts)
    vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts)
    vim.keymap.set('n', '<space>f', function() vim.lsp.buf.format { async = true } end, bufopts)
end

-- Try to use mason-lspconfig if available, otherwise setup servers manually
local mason_lspconfig_ok, mason_lspconfig = pcall(require, 'mason-lspconfig')
if mason_lspconfig_ok then
    -- Setup mason-lspconfig
    mason_lspconfig.setup({
        ensure_installed = {}, -- Add LSP servers here as needed
        automatic_installation = false,
    })
    
    -- Setup handlers if the API is available
    if mason_lspconfig.setup_handlers then
        mason_lspconfig.setup_handlers({
            -- Default handler for all servers
            function(server_name)
                lspconfig[server_name].setup({
                    capabilities = capabilities,
                    on_attach = on_attach,
                })
            end,
        })
    end
end

-- Setup LSP servers manually (uncomment as needed)
-- Install servers via :Mason command in Neovim first

-- Lua LSP
-- lspconfig.lua_ls.setup({
--     capabilities = capabilities,
--     on_attach = on_attach,
--     settings = {
--         Lua = {
--             runtime = { version = 'LuaJIT' },
--             diagnostics = { globals = { 'vim' } },
--             workspace = { library = vim.api.nvim_get_runtime_file("", true) },
--             telemetry = { enable = false },
--         },
--     },
-- })

-- Python LSP (uncomment when pyright is installed)
-- lspconfig.pyright.setup({
--     capabilities = capabilities,
--     on_attach = on_attach,
-- })

-- Rust LSP (uncomment when rust_analyzer is installed)
-- lspconfig.rust_analyzer.setup({
--     capabilities = capabilities,
--     on_attach = on_attach,
-- })

-- Diagnostic configuration
vim.diagnostic.config({
    virtual_text = true,
    signs = true,
    update_in_insert = false,
    underline = true,
    severity_sort = true,
    float = {
        focusable = false,
        style = "minimal",
        border = "rounded",
        source = "always",
        header = "",
        prefix = "",
    },
})

-- Show diagnostic signs
local signs = { Error = "󰅚 ", Warn = "󰀪 ", Hint = "󰌶 ", Info = "󰋽 " }
for type, icon in pairs(signs) do
    local hl = "DiagnosticSign" .. type
    vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" })
end
