require("ink.remap")
require("ink.tabline")
require("ink.lsp")

vim.opt.ignorecase = true -- ignore case in searches by default
vim.opt.smartcase = true -- but make it case sensitive if an uppercase is entered

vim.wo.number = true
vim.opt.relativenumber = true
vim.wo.signcolumn = "yes" -- Keep signcolumn on by default

vim.opt.swapfile = false -- creates a swapfile
vim.opt.undofile = true -- Save undo history
-- if a file is being edited by another program (or was written to file while editing with another program), it is not
-- allowed to be edited
vim.opt.writebackup = false

vim.opt.mouse = "a"
vim.opt.clipboard = "unnamedplus"

vim.o.linebreak = true -- 2. Break lines at word boundaries (improves readability)
vim.o.showbreak = "↪ " -- 3. Add a visual indicator for wrapped lines
vim.opt.scrolloff = 4 -- minimal number of screen lines to keep above and below the cursor
vim.opt.sidescrolloff = 8 -- minimal number of screen columns either side of cursor if wrap is `false`
vim.opt.numberwidth = 2 -- set number column width to 2 {default 4}
vim.opt.shiftwidth = 4 -- Number of spaces inserted when indenting
vim.opt.tabstop = 4 -- A TAB character looks like 4 spaces
vim.opt.softtabstop = 4 -- Number of spaces inserted instead of a TAB character
vim.opt.expandtab = true -- Pressing the TAB key will insert spaces instead of a TAB character
vim.opt.breakindent = true -- Enable break indent
vim.opt.smartindent = true -- make indenting smarter again
vim.opt.whichwrap = "bs<>[]hl" -- which "horizontal" keys are allowed to travel to prev/next line

vim.opt.updatetime = 250 -- Decrease update time
vim.opt.timeoutlen = 300 -- time to wait for a mapped sequence to complete (in milliseconds)

vim.o.completeopt = "menuone,noinsert,popup,fuzzy"
-- Replicate blink's cmdline configuration
vim.opt.wildmode = "longest:full,full"
vim.opt.wildoptions = { "pum", "fuzzy" } -- Use a popup menu for cmdline with fuzzy matching

vim.opt.cursorline = true
vim.opt.termguicolors = true

vim.opt.splitbelow = true -- open new vertical split bottom
vim.opt.splitright = true -- open new horizontal splits right

vim.opt.pumheight = 10 -- pop up menu height

vim.opt.shortmess:append("c") -- don't give |ins-completion-menu| messages
vim.opt.iskeyword:append("-") -- hyphenated words recognized by searches
-- don't insert the current comment leader automatically for auto-wrapping comments using 'textwidth', hitting <Enter>
-- in insert mode, or hitting 'o' or 'O' in normal mode.
vim.opt.formatoptions:remove({ "c", "r", "o" })
vim.opt.runtimepath:remove("/usr/share/vim/vimfiles") -- separate vim plugins from neovim in case vim still in use

-- Nice and simple folding:
vim.opt.foldenable = true
vim.opt.foldlevel = 99
vim.opt.fillchars:append({ fold = ">" })

-- vim.o.autocomplete = true -- Enables the overall completion feature.

-- Auto-reload buffers when files change on disk (e.g. from external tools or AI agents)
vim.api.nvim_create_autocmd({ "FocusGained", "BufEnter", "CursorHold" }, {
	callback = function()
		if vim.fn.getcmdwintype() == "" then
			vim.cmd("checktime")
		end
	end,
})

-- remove trailing whitespace (keeppatterns avoids adding to search history)
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
	pattern = { "*" },
	callback = function()
		local save_cursor = vim.fn.getpos(".")
		pcall(function()
			vim.cmd([[keeppatterns %s/\s\+$//e]])
		end)
		vim.fn.setpos(".", save_cursor)
	end,
})
--
-- Set a keymap to toggle the 'wrap' option
vim.keymap.set("n", "<leader>w", function()
	vim.opt.wrap = not vim.opt.wrap:get()
end, { desc = "Toggle line wrapping" })

local uv = vim.uv

vim.api.nvim_create_autocmd({ "VimEnter", "VimLeave" }, {
	callback = function()
		if vim.env.TMUX_PLUGIN_MANAGER_PATH then
			uv.spawn(vim.env.TMUX_PLUGIN_MANAGER_PATH .. "/tmux-window-name/scripts/rename_session_windows.py", {})
		end
	end,
})
--
-- Filetypes to enable spellcheck
local spell_types = { "text", "plaintex", "typst", "gitcommit", "markdown" }

-- add gotmpl filetypes for blueprint repos
-- *.ext.tmpl files use the filetype of ext (e.g. foo.json.tmpl -> json, bar.sh.tmpl -> bash)
vim.filetype.add({
	extension = {
		gotmpl = "gotmpl",
	},
	pattern = {
		[".*/recipes/.*%.ya?ml"] = "gotmpl",
		[".*%.tmpl$"] = function(path)
			local name = vim.fn.fnamemodify(path, ":t")
			local base = name:match("^(.+)%.tmpl$")
			if base then
				local ext = base:match("%.(%w+)$")
				if ext then
					if ext == "sh" then
						return "bash"
					end
					return ext
				end
			end
			return nil
		end,
	},
})

-- Set global spell option to false initially to disable it for all file types
vim.opt.spell = false

-- Create an augroup for spellcheck to group related autocommands
vim.api.nvim_create_augroup("Spellcheck", { clear = true })

-- Create an autocommand to enable spellcheck for specified file types
vim.api.nvim_create_autocmd({ "FileType" }, {
	group = "Spellcheck", -- Grouping the command for easier management
	pattern = spell_types, -- Only apply to these file types
	callback = function()
		vim.opt_local.spell = true -- Enable spellcheck for these file types
	end,
	desc = "Enable spellcheck for defined filetypes", -- Description for clarity
})
