-- ~/.config/nvim/after/plugin/toggleterm.lua

local status_ok, toggleterm = pcall(require, "toggleterm")
if not status_ok then
  return
end

toggleterm.setup({
  open_mapping = [[<c-\>]],
  start_in_insert = true,
  persist_size = true,


  size = function(term)
    if term.direction == "vertical" then
      return vim.o.columns * 0.37 -- Set to 50% of the screen width
    elseif term.direction == "horizontal" then
      return 10 -- A height of 15 lines
    end
  end,

  direction = 'float', -- Default direction
  close_on_exit = true,
  shade_terminals = true,
  shading_factor = 2,
  float_opts = {
    border = 'curved',
    winblend = 0,
  },
  winbar = {
    enabled = true,
    name_formatter = function(term)
      return term.name
    end,
  },
  on_open = function(term)
    vim.cmd("setlocal nonumber norelativenumber")
    -- Don't auto-start insert for UI tools


    -- New, robust code
    if not term.cmd or (not string.find(term.cmd, "lazygit") and not string.find(term.cmd, "yazi")) then
        vim.cmd("startinsert!")
    end

    local opts = { buffer = term.bufnr }
    vim.keymap.set('t', '<esc>', [[<C-\><C-n>]], opts)
    vim.keymap.set('t', '<C-h>', [[<Cmd>wincmd h<CR>]], opts)
    vim.keymap.set('t', '<C-j>', [[<Cmd>wincmd j<CR>]], opts)
    vim.keymap.set('t', '<C-k>', [[<Cmd>wincmd k<CR>]], opts)
    vim.keymap.set('t', '<C-l>', [[<Cmd>wincmd l<CR>]], opts)
  end,
})

-- Helper function for custom terminals
function _G.set_terminal_toggle(cmd)
  local Terminal = require('toggleterm.terminal').Terminal
  local term = Terminal:new({
    cmd = cmd,
    dir = "git_dir",
    direction = "float",
    float_opts = { border = "double" },
    hidden = true,
    on_open = function(t)
      -- Allow quitting UI with 'q'
      vim.api.nvim_buf_set_keymap(t.bufnr, "n", "q", "<cmd>close<CR>", { noremap = true, silent = true })
    end
  })
  return function()
    term:toggle()
  end
end

-- Create toggles
local lazygit_toggle = set_terminal_toggle('lazygit')
local yazi_toggle = set_terminal_toggle('yazi')
local ranger_toggle = set_terminal_toggle('ranger')
local python_toggle = set_terminal_toggle('python')
local node_toggle = set_terminal_toggle('node')
local htop_toggle = set_terminal_toggle('htop')

-- Keybindings for custom terminals
vim.keymap.set("n", "<leader>ttl", lazygit_toggle, { desc = "Toggleterm: Lazygit" })
vim.keymap.set("n", "<leader>tty", yazi_toggle, { desc = "Toggleterm: Yazi" })
vim.keymap.set("n", "<leader>ttr", ranger_toggle, { desc = "Toggleterm: Ranger" })
vim.keymap.set("n", "<leader>ttp", python_toggle, { desc = "Toggleterm: Python REPL" })
vim.keymap.set("n", "<leader>ttN", node_toggle, { desc = "Toggleterm: Node REPL" })
vim.keymap.set("n", "<leader>ttt", htop_toggle, { desc = "Toggleterm: Htop" })

-- Keybindings for different layouts and actions
vim.keymap.set("n", "<leader>ttf", "<cmd>ToggleTerm direction=float<CR>", { desc = "Toggleterm: Float" })
vim.keymap.set("n", "<leader>ttv", "<cmd>ToggleTerm direction=vertical name='Process'<CR>", { desc = "Toggleterm: Vertical Process" })
vim.keymap.set("n", "<leader>tth", "<cmd>ToggleTerm direction=horizontal name='REPL'<CR>", { desc = "Toggleterm: Horizontal REPL" })

-- Open a NEW terminal
vim.keymap.set("n", "<leader>ttn", "<cmd>TermNew<CR>", { desc = "Toggleterm: New Terminal" })

-- Select between open terminals
vim.keymap.set("n", "<leader>T", "<cmd>TermSelect<CR>", { desc = "Toggleterm: Select" })

-- Send code to the REPL terminal
function _G.send_to_repl()
  local selection = require('toggleterm.utils').get_visual_selection()
  require('toggleterm').exec(selection, 0, 0, 'REPL')
end
vim.keymap.set("v", "<leader>tts", send_to_repl, { desc = "Toggleterm: Send to REPL" })
