-- NvimConfigStart

vim.g.mapleader = ' '
vim.g.maplocalleader = ' '

vim.g.have_nerd_font = false
vim.opt.number = true
vim.opt.mouse = 'a'
vim.opt.showmode = false

vim.schedule(function()
        vim.opt.clipboard = 'unnamedplus'
end)

vim.opt.breakindent = true
vim.opt.undofile = true
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.signcolumn = 'yes'

vim.opt.updatetime = 250
vim.opt.timeoutlen = 300

vim.opt.splitright = true
vim.opt.splitbelow = true

vim.opt.list = true
vim.opt.listchars = { tab = '» ', trail = '·', nbsp = '␣' }
vim.opt.inccommand = 'split'
vim.opt.cursorline = true
vim.opt.scrolloff = 18

vim.api.nvim_create_autocmd('TextYankPost', {
        desc = 'Highlight when yanking (copying) text',
        group = vim.api.nvim_create_augroup('kickstart-highlight-yank', { clear = true }),
        callback = function()
                vim.highlight.on_yank()
        end,
})

-- Mini.Deps

local path_package = vim.fn.stdpath('data') .. '/site/'
local mini_path = path_package .. 'pack/deps/start/mini.nvim'
if not vim.loop.fs_stat(mini_path) then
        vim.cmd('echo "Installing `mini.nvim`" | redraw')
        local clone_cmd = {
                'git', 'clone', '--filter=blob:none',
                'https://github.com/echasnovski/mini.nvim', mini_path
        }
        vim.fn.system(clone_cmd)
        vim.cmd('packadd mini.nvim | helptags ALL')
        vim.cmd('echo "Installed `mini.nvim`" | redraw')
end

require('mini.deps').setup({ path = { package = path_package } })

local add = MiniDeps.add

add({ source = 'sainnhe/gruvbox-material' })
add({
        source = 'nvim-treesitter/nvim-treesitter',
        checkout = 'master',
        monitor = 'main',
        hooks = { post_checkout = function() vim.cmd('TSUpdate') end },
})
add({
	source = 'vimwiki/vimwiki',
	depends = {},
	config = function()
		vim.g.vimwiki_list = {
			{
				path = '~/vimwiki/',
				syntax = 'markdown',
				ext = '.md'
			}
		}
		-- Optional: Disable using vimwiki for non-wiki markdown files
		vim.g.vimwiki_global_ext = 0
	end
})
add({
        source = 'neovim/nvim-lspconfig',
        depends = { 'williamboman/mason.nvim' },
})
add({
        source = 'hrsh7th/nvim-cmp',
        depends = { 'hrsh7th/cmp-nvim-lsp' },
})
add({
        source = 'nvim-telescope/telescope.nvim',
        depends = { 'nvim-lua/plenary.nvim' },
})
add({
        source = 'williamboman/mason.nvim',
        depends = {},
})

local now, later = MiniDeps.now, MiniDeps.later

now(function()
        vim.o.background = 'dark'
        vim.cmd.colorscheme('gruvbox-material')
end)
now(function() require('mini.statusline').setup() end)
now(function() require('mini.icons').setup() end)

later(function() require('mini.surround').setup() end)
later(function() require('mini.tabline').setup() end)
later(function() require('mini.move').setup() end)
later(function() require('mini.jump2d').setup() end)
later(function() require('mini.jump').setup() end)
later(function() require('mini.bracketed').setup() end)
later(function() require('mini.ai').setup() end)

later(function()
        require('nvim-treesitter.configs').setup({
                ensure_installed = { 'lua', 'vimdoc', 'python', 'go' },
                highlight = { enable = true },
        })
end)

-- Completion Setup
later(function()
local cmp = require('cmp')
cmp.setup({
        mapping = {
                ['<C-k>'] = cmp.mapping.select_next_item(),
                ['<C-j>'] = cmp.mapping.select_prev_item(),
                ['<C-Space>'] = cmp.mapping.complete(),
                ['<CR>'] = cmp.mapping.confirm({ select = true }),
        },
        sources = {
                { name = 'nvim_lsp' },
        },
})
end)

-- Telescope Keybindings
vim.keymap.set('n', '<Leader>f', require('telescope.builtin').find_files, { noremap = true, silent = true })
vim.keymap.set('n', '<Leader> ', require('telescope.builtin').live_grep, { noremap = true, silent = true })
vim.keymap.set('n', '<Leader>b', require('telescope.builtin').buffers, { noremap = true, silent = true })
vim.keymap.set('n', '<Leader>fh', require('telescope.builtin').help_tags, { noremap = true, silent = true })

-- LSP Key Mappings
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, { buffer = 0 })
vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, { buffer = 0 })
vim.keymap.set('n', '<Leader>ca', vim.lsp.buf.code_action, { buffer = 0 })
vim.keymap.set('n', '<Leader>r', vim.lsp.buf.rename, { buffer = 0 })
vim.keymap.set('n', '<Leader>df', vim.diagnostic.goto_next, { buffer = 0 })
vim.keymap.set('n', '<Leader>dp', vim.diagnostic.goto_prev, { buffer = 0 })

-- LSP and Mason Setup
later(function()
        require('mason').setup()
        require('lspconfig').lua_ls.setup({
                settings = {
                        Lua = {
                                diagnostics = {
                                        globals = { 'vim', 'MiniDeps' }
                                }
                        }
                }
        })

        require('lspconfig').pyright.setup({})
        require('lspconfig').gopls.setup({})
end)

-- Misc Keybindings
vim.keymap.set('n', '<Esc>', '<cmd>nohlsearch<CR>')
vim.keymap.set('n', '<Leader>s', ':w<CR>', { noremap = true, silent = true })
vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist)
vim.keymap.set('t', '<Esc><Esc>', '<C-\\><C-n>', { desc = 'Exit terminal mode' })

vim.keymap.set('n', 'j', 'gj', { noremap = true })
vim.keymap.set('n', 'k', 'gk', { noremap = true })

-- Vimwiki Configuration
vim.keymap.set('n', '<leader>ww', ':VimwikiIndex<CR>', { noremap = true, silent = true })
vim.keymap.set('n', '<leader>wd', ':VimwikiMakeDiaryNote<CR>', { noremap = true, silent = true })

-- End NvimConfig

format it and add conform and luasnip