local lazypath = vim.fn.stdpath("data") .. "/site/pack/packer/start/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
	vim.fn.system({
		"git",
		"clone",
		"https://github.com/folke/lazy.nvim.git",
		lazypath,
	})
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
	{
		"windwp/nvim-autopairs",
		event = "InsertEnter",
		config = function()
			require("nvim-autopairs").setup({})
		end,
	},
	{
		"nvim-treesitter/nvim-treesitter",
		build = ":TSUpdate",
		lazy = false,
	},
	{
		"rose-pine/neovim",
		name = "rose-pine",
		config = function()
			vim.cmd("colorscheme rose-pine")
		end,
	},
	{
		"stevearc/conform.nvim",
		opts = {},
	},
	{
		"stevearc/oil.nvim",
		opts = {},
	},
	{
		"numToStr/Comment.nvim",
		opts = {},
		lazy = false,
	},
	{
		"folke/which-key.nvim",
		event = "VeryLazy",
		init = function()
			vim.o.timeout = true
			vim.o.timeoutlen = 300
		end,
		opts = {
			preset = "helix",
		},
	},
	{
		"kylechui/nvim-surround",
		version = "main",
		event = "VeryLazy",
		config = function()
			require("nvim-surround").setup({})
		end,
	},
	{ "lewis6991/gitsigns.nvim" },
	{ "neovim/nvim-lspconfig" },
	{ "hrsh7th/nvim-cmp" },
	{ "hrsh7th/cmp-nvim-lsp" },
	{ "hrsh7th/cmp-buffer" },
	{ "hrsh7th/cmp-path" },
	{ "saadparwaiz1/cmp_luasnip" },
	{ "L3MON4D3/LuaSnip" },
	{ "rafamadriz/friendly-snippets" },
})

local cmp = require("cmp")

cmp.setup({
	snippet = {
		expand = function(args)
			require("luasnip").lsp_expand(args.body) -- Use LuaSnip for snippets
		end,
	},
	mapping = {
		["<S-Tab>"] = cmp.mapping.select_prev_item(),
		["<Tab>"] = cmp.mapping.select_next_item(),
		["<C-d>"] = cmp.mapping.scroll_docs(-4),
		["<C-f>"] = cmp.mapping.scroll_docs(4),
		-- ["<C-Space>"] = cmp.mapping.complete(),
		["<C-Space>"] = cmp.mapping.close(),
		["<CR>"] = cmp.mapping.confirm({ select = true }),
	},
	sources = {
		{ name = "path" }, -- Path completion
		{ name = "luasnip" }, -- Snippet completion
		{ name = "nvim_lsp" }, -- LSP completion
		{ name = "buffer" }, -- Buffer completion
	},
})

require("nvim-treesitter.configs").setup({
	ensure_installed = {
		"go",
		"html",
		"css",
		"javascript",
		"hyprlang",
		"sql",
		"gomod",
		"gosum",
		"bash",
		"c",
		"rust",
		"lua",
		"nginx",
	},
	highlight = {
		enable = true,
	},
})

require("oil").setup({
	default_file_explorer = false,
})

-- nvim-lspconfig - Providing basic, default Nvim LSP client configurations for various LSP servers.
local lspconfig = require("lspconfig")
lspconfig.pyright.setup({})
lspconfig.ts_ls.setup({})
lspconfig.gopls.setup({})
lspconfig.bashls.setup({})
lspconfig.arduino_language_server.setup({})
lspconfig.clangd.setup({})
lspconfig.cssls.setup({})
lspconfig.css_variables.setup({})
local luasnip = require("luasnip")
require("luasnip.loaders.from_vscode").lazy_load()
require("luasnip").config.set_config({
	history = true,
	updateevents = "TextChanged,TextChangedI",
})

-- native vim options
local vim = vim
local opt = vim.opt

-- opt.foldmethod = "expr"
-- opt.foldexpr = "nvim_treesitter#foldexpr()"

vim.wo.number = true
vim.wo.relativenumber = true

vim.keymap.set("n", "<C-tab>", "gt")
vim.keymap.set("n", "<CS-tab>", "gT")

vim.opt.langmap:append("-fsrbkuiopxtedgyhnjawlcvqm';qwertyuiopasdfgljkhzxcvbnm\\;")

opt.tabstop = 2
opt.shiftwidth = 2
opt.expandtab = true

vim.keymap.set("ca", "o", "tabnew")

-- Comment.vim - Smart and Powerful commenting plugin for neovim
require("Comment").setup()

-- conform.nvim - Lightweight yet powerful formatter plugin for neovim
require("conform").setup({
	formatters_by_ft = {
		lua = { "stylua" },
		python = { "isort", "black" },
		rust = { "rustfmt" },
		javascript = { "prettier" },
		html = { "prettier" },
		go = { "gofmt" },
		bash = { "shfmt" },
		nginx = { "nginxfmt" },
		json = { "fixjson" },
	},
	format_on_save = {
		timeout_ms = 500,
		lsp_format = "fallback",
	},
})

-- Hyprlang LSP
vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter" }, {
	pattern = { "*.hl", "hypr*.conf" },
	callback = function(event)
		vim.lsp.start({
			name = "hyprlang",
			cmd = { "hyprls" },
			root_dir = vim.fn.getcwd(),
		})
	end,
})

-- enable diagnostics
vim.diagnostic.config({
	virtual_text = {
		prefix = "▎",
	},
	severity_sort = true,
	float = {
		source = "always",
	},
})

-- save and format on insertleave
vim.api.nvim_create_autocmd({ --[["InsertLeave",]]
	"FocusLost",
}, {
	pattern = "*",
	callback = function()
		require("conform").format()
		vim.cmd("silent! wa")
	end,
})
