-- Minimal server editor config
-- Launch with: NVIM_APPNAME=nvim-server nvim

-------------------- Options --------------------
vim.g.mapleader = ","
vim.opt.number = true
vim.opt.cursorline = true
vim.opt.expandtab = true
vim.opt.tabstop = 4
vim.opt.shiftwidth = 4
vim.opt.softtabstop = 4
vim.opt.autoindent = true
vim.opt.smartindent = true
vim.opt.hlsearch = true
vim.opt.incsearch = true
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.splitright = true
vim.opt.splitbelow = true
vim.opt.termguicolors = true
vim.opt.scrolloff = 8
vim.opt.signcolumn = "no"
vim.opt.updatetime = 250
vim.opt.undofile = true

-------------------- Clipboard --------------------
vim.opt.clipboard = 'unnamedplus'

vim.g.clipboard = {
  name = 'OSC 52',
  copy = {
    ['+'] = require('vim.ui.clipboard.osc52').copy('+'),
    ['*'] = require('vim.ui.clipboard.osc52').copy('*'),
  },
  paste = {
    ['+'] = require('vim.ui.clipboard.osc52').paste('+'),
    ['*'] = require('vim.ui.clipboard.osc52').paste('*'),
  },
}
vim.keymap.set("v", "<leader>y", '"+y')
vim.keymap.set("n", "<leader>Y", '"+yg_')
vim.keymap.set("n", "<leader>y", '"+y')
vim.keymap.set("n", "<leader>yy", '"+yy')
vim.keymap.set("n", "<leader>p", '"+p')
vim.keymap.set("n", "<leader>P", '"+P')

-------------------- Keymaps --------------------
vim.keymap.set("n", "i", ":nohls<CR>i", { silent = true })
vim.keymap.set("n", "<Space>", ":w<CR>", { silent = true })

-- Window navigation
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")
vim.keymap.set("n", "<C-l>", "<C-W>l")
vim.keymap.set("n", "<C-Left>", "<C-W>h")
vim.keymap.set("n", "<C-Down>", "<C-W>j")
vim.keymap.set("n", "<C-Up>", "<C-W>k")
vim.keymap.set("n", "<C-Right>", "<C-W>l")

-- Tab management
vim.keymap.set("n", "<leader>to", ":tabonly<CR>", { silent = true })
vim.keymap.set("n", "<leader>o", ":only<CR>", { silent = true })
vim.keymap.set("n", "<leader><Tab>", ":tabn<CR>", { silent = true })
vim.keymap.set("n", "<S-Tab>", ":tabp<CR>", { silent = true })


-------------------- Netrw (built-in file browser) --------------------
vim.g.netrw_banner = 0
vim.g.netrw_liststyle = 3
vim.keymap.set("n", "<F2>", ":Explore<CR>", { silent = true })

-------------------- Statusline (built-in) --------------------
vim.opt.statusline = " %f %m%r%= %y  %l:%c  %p%% "

-------------------- Bootstrap lazy.nvim --------------------
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if vim.fn.isdirectory(lazypath) == 0 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)

require("lazy").setup({
    { "4ree/viml-surround", event = "VeryLazy" },
    { "numToStr/Comment.nvim", event = "VeryLazy", opts = {} },
    {
        "junegunn/fzf.vim",
        dependencies = { "junegunn/fzf" },
        keys = {
            { "<leader>f", "<cmd>Files<CR>", desc = "Find files" },
            { "<leader>b", "<cmd>Buffers<CR>", desc = "Buffers" },
            { "<leader>r", "<cmd>Rg<CR>", desc = "Ripgrep" },
            { "<leader>g", "<cmd>GFiles<CR>", desc = "Git files" },
        },
    },
})

-------------------- Grep (use ripgrep if available) --------------------
if vim.fn.executable("rg") == 1 then
    vim.opt.grepprg = "rg --vimgrep --smart-case"
    vim.opt.grepformat = "%f:%l:%c:%m"
end
