require("vim._core.ui2").enable({
	enable = true,
})

vim.opt.cmdheight = 2 -- more space in the neovim command line for displaying messages

vim.pack.add({
	{ src = "https://github.com/rose-pine/neovim" },
	{ src = "https://github.com/brenoprata10/nvim-highlight-colors" },
})

-- Sync background with tmux rose-pine variant
if vim.env.TMUX then
	local variant = vim.fn.system("tmux show-option -gqv @rose_pine_variant"):gsub("%s+", "")
	vim.o.background = variant == "dawn" and "light" or "dark"
else
	vim.o.background = "dark"
end

vim.cmd.colorscheme("rose-pine")

vim.keymap.set("n", "<leader>tt", function()
	vim.o.background = vim.o.background == "dark" and "light" or "dark"
end, { desc = "Alternate between light and dark mode" })

-- Insert-mode cursor: extmark overlay for full fg+bg control (terminal ignores fg on cursor hl groups)
local function set_icursor_hl()
	local palette = require("rose-pine.palette")
	vim.api.nvim_set_hl(0, "iCursorChar", { fg = palette.surface, bg = palette.love })
end

set_icursor_hl()
vim.api.nvim_create_autocmd({ "ColorScheme", "OptionSet" }, {
	pattern = { "*", "background" },
	callback = set_icursor_hl,
})

local icursor_ns = vim.api.nvim_create_namespace("icursor")

local function update_icursor()
	vim.api.nvim_buf_clear_namespace(0, icursor_ns, 0, -1)
	local r, c = unpack(vim.api.nvim_win_get_cursor(0))
	local line = vim.api.nvim_get_current_line()
	local char = c < #line and line:sub(c + 1, c + 1) or " "
	vim.api.nvim_buf_set_extmark(0, icursor_ns, r - 1, c, {
		virt_text = { { char, "iCursorChar" } },
		virt_text_pos = "overlay",
	})
end

vim.api.nvim_create_autocmd("ModeChanged", { pattern = "*:i*", callback = update_icursor })
vim.api.nvim_create_autocmd("CursorMovedI", { callback = update_icursor })
vim.api.nvim_create_autocmd("ModeChanged", {
	pattern = "i*:*",
	callback = function()
		vim.api.nvim_buf_clear_namespace(0, icursor_ns, 0, -1)
	end,
})

require("nvim-highlight-colors").setup()

vim.pack.add({
	{ src = "https://github.com/nvim-tree/nvim-web-devicons" },
	{ src = "https://github.com/nvim-lualine/lualine.nvim" },
	{ src = "https://github.com/lukas-reineke/indent-blankline.nvim" },
})

require("lualine").setup({
	options = {
		section_separators = "",
		component_separators = "",
		theme = "rose-pine",
	},
	extensions = { "fugitive", "lazy" },
})

require("ibl").setup()

vim.pack.add({
	{ src = "https://github.com/lewis6991/gitsigns.nvim" },
	{ src = "https://github.com/f-person/git-blame.nvim" },
})

require("gitsigns").setup({
	signs = {
		add = { text = "+" },
		change = { text = "~" },
		delete = { text = "_" },
		topdelete = { text = "‾" },
		changedelete = { text = "~" },
	},
	signs_staged = {
		add = { text = "+" },
		change = { text = "~" },
		delete = { text = "_" },
		topdelete = { text = "‾" },
		changedelete = { text = "~" },
	},
})

vim.keymap.set("n", "<leader>gk", "<cmd>GitBlameToggle<cr>", { desc = "Toggle git-blame" })
vim.g.gitblame_display_virtual_text = false
--	schedule_event = "CursorHold"
--	clear_event = "CursorHoldI"
vim.g.gitblame_delay = 50 -- miliseconds
local git_blame = require("gitblame")
require("lualine").setup({
	sections = {
		lualine_c = {
			{ "filename" },
			{ git_blame.get_current_blame_text, cond = git_blame.is_blame_text_available },
		},
	},
})
