vim.pack.add({
	{ src = "https://github.com/carlos-algms/agentic.nvim" },
	{
		src = "https://github.com/saghen/blink.cmp",
		version = vim.version.range("^1"),
	},
})

-- Chat interface over ACP. This config is shared across workstations that allow
-- different agents, so pick whichever bridge is actually on PATH, in order:
--   claude-agent-acp -- npm i -g @agentclientprotocol/claude-agent-acp
--   kiro-cli         -- curl -fsSL https://cli.kiro.dev/install | bash
-- Neither takes an API key; auth is inherited from that agent's own CLI login
-- (`claude /login` / `kiro-cli login`).
local acp_bridges = {
	{ provider = "claude-agent-acp", command = "claude-agent-acp" },
	{ provider = "kiro-acp", command = "kiro-cli" },
}

local function detect_provider()
	for _, bridge in ipairs(acp_bridges) do
		if vim.fn.executable(bridge.command) == 1 then
			return bridge.provider
		end
	end
	-- none installed: agentic shows its own "not installed" warning on first use
	return acp_bridges[1].provider
end

require("agentic").setup({
	provider = detect_provider(),
	-- only offer the bridges that are installed, not all thirteen
	provider_switcher = { hide_unhealthy_providers = true },
})

vim.keymap.set({ "n", "v" }, "<leader>a", function()
	require("agentic").toggle()
end, { desc = "Toggle Agentic chat" })

vim.keymap.set({ "n", "v" }, "<leader>ca", function()
	require("agentic").add_selection_or_file_to_context()
end, { desc = "Add selection or file to Agentic context" })

vim.keymap.set("n", "<leader>cn", function()
	require("agentic").new_session()
end, { desc = "Start a new Agentic session" })

vim.cmd("packadd blink.cmp")
local blink = require("blink.cmp")
blink.setup({
	keymap = { preset = "default" },
	appearance = { nerd_font_variant = "mono" },
	completion = {
		menu = { border = "rounded" },
		documentation = { auto_show = true, window = { border = "rounded" } },
		ghost_text = { enabled = true }, -- Enables ghost text in insert mode
	},

	cmdline = {
		keymap = { ["<Tab>"] = { "show", "accept" } },
		completion = { menu = { auto_show = true }, ghost_text = { enabled = true } },
	},

	-- By removing `snippets = { preset = 'luasnip' }`, Blink natively
	-- defaults to Neovim 0.11+ built-in `vim.snippet` API.

	sources = {
		default = { "lsp", "path", "snippets", "buffer" },
		providers = {
			cmdline = {
				min_keyword_length = function(ctx)
					if ctx.mode == "cmdline" and string.find(ctx.line, " ") == nil then
						return 3
					end
					return 0
				end,
			},
		},
	},
	fuzzy = { implementation = "prefer_rust_with_warning" },
})

-- Ensure borders stay colored even after toggling light/dark themes
vim.api.nvim_create_autocmd("ColorScheme", {
	pattern = "*",
	callback = function()
		vim.api.nvim_set_hl(0, "BlinkCmpMenuBorder", { link = "Special" })
		vim.api.nvim_set_hl(0, "BlinkCmpDocBorder", { link = "Special" })
		vim.api.nvim_set_hl(0, "FloatBorder", { link = "Special" })
	end,
})

local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = blink.get_lsp_capabilities(capabilities)

-- Safely merge with existing global LSP capabilities so we don't overwrite settings from ink/lsp.lua
local existing_config = vim.lsp.config["*"]
if type(existing_config) == "table" and type(existing_config.capabilities) == "table" then
	capabilities = vim.tbl_deep_extend("force", existing_config.capabilities, capabilities)
end

-- Apply to all natively loaded 0.12 LSP servers
vim.lsp.config("*", {
	capabilities = capabilities,
	-- Note: We DO NOT use vim.lsp.completion.enable() here because
	-- Blink is taking over the UI rendering.
})
