-------------------------------------------------------------------------------
-- 1. Leader Keys & Timeout Options
-------------------------------------------------------------------------------
vim.keymap.set("", "<Space>", "Nop")
vim.g.mapleader = ' '
vim.g.maplocalleader = ' '

-------------------------------------------------------------------------------
-- 2. Built-in Plugin Manager (vim.pack)
-------------------------------------------------------------------------------
vim.pack.add({
  'https://github.com/RomanAverin/charleston.nvim',
  'https://github.com/nvim-tree/nvim-web-devicons',
  'https://github.com/nvim-lualine/lualine.nvim',
  'https://github.com/stevearc/oil.nvim',
  'https://github.com/nvim-lua/plenary.nvim',
  'https://github.com/nvim-telescope/telescope.nvim',
  'https://github.com/nvimdev/dashboard-nvim',
})

-------------------------------------------------------------------------------
-- 3. Options
-------------------------------------------------------------------------------
local opt = vim.opt

opt.showmode = false
opt.number = true 
opt.relativenumber = true 
opt.tabstop = 4 
opt.shiftwidth = 4 
opt.expandtab = true 
opt.autoindent = true 
opt.smartindent = true 
opt.background = "dark" 
opt.signcolumn = "yes" 
opt.cursorline = true 
opt.listchars = "tab: ,multispace:|   ,eol:󰌑" 
opt.list = true 
opt.clipboard = "unnamedplus"

opt.timeoutlen = 2000
-- opt.timeout = false
-------------------------------------------------------------------------------
-- 4. Plugin Configurations & Colorscheme
-------------------------------------------------------------------------------
-- Colorscheme
vim.cmd.colorscheme('charleston')

-- Lualine
require('lualine').setup({
  options = {
    theme = 'charleston',
    component_separators = { left = '', right = '' },
    section_separators = { left = '', right = '' },
    globalstatus = true,
  },
  sections = {
    lualine_a = { 'mode' },
    lualine_b = { 'branch', 'diff', 'diagnostics' },
    lualine_c = { 'filename' },
    lualine_x = { 'encoding', 'fileformat', 'filetype' },
    lualine_y = { 'progress' },
    lualine_z = { 'location' },
  },
})

-- Oil
require('oil').setup({
  default_file_explorer = true,
  delete_to_trash = true,
  skip_confirm_for_simple_edits = true,
  view_options = {
    show_hidden = true,
  },
})

-- Telescope
require('telescope').setup({
  defaults = {
    path_display = { "smart" },
    file_ignore_patterns = { "node_modules", ".git/" },
  },
})

-- Dashboard
require('dashboard').setup({
  theme = 'doom',
  config = {
    header = {
                  "                                                       ",
                  " ███╗   ██╗ ███████╗ ██████╗  ██╗   ██╗ ██╗ ███╗   ███╗",
                  " ████╗  ██║ ██╔════╝██╔═══██╗ ██║   ██║ ██║ ████╗ ████║",
                  " ██╔██╗ ██║ █████╗  ██║   ██║ ██║   ██║ ██║ ██╔████╔██║",
                  " ██║╚██╗██║ ██╔══╝  ██║   ██║ ╚██╗ ██╔╝ ██║ ██║╚██╔╝██║",
                  " ██║ ╚████║ ███████╗╚██████╔╝  ╚████╔╝  ██║ ██║ ╚═╝ ██║",
                  " ╚═╝  ╚═══╝ ╚══════╝ ╚═════╝    ╚═══╝   ╚═╝ ╚═╝     ╚═╝",
                  "                                                       ",
    },
    center = {
      { icon = ' ', desc = 'Find File          ', action = 'Telescope find_files', key = 'f' },
      { icon = ' ', desc = 'Find Text          ', action = 'Telescope live_grep',  key = 't' },
      { icon = ' ', desc = 'Recent Files       ', action = 'Telescope oldfiles',   key = 'r' },
      { icon = ' ', desc = 'File Explorer      ', action = 'Oil',                  key = 'e' },
      { icon = ' ', desc = 'New File           ', action = 'ene | startinsert',    key = 'n' },
      { icon = '󰈆 ', desc = 'Quit Neovim        ', action = 'qa',                   key = 'q' },
    },
    footer = { "", "⚡ Neovim 0.12 Minimal Config" },
  },
})

-------------------------------------------------------------------------------
-- 5. Keymaps
-------------------------------------------------------------------------------
local keymap = vim.keymap.set
local opts = { silent = true }

-- File & Quit shortcuts
keymap('n', '<leader>w', '<cmd>write<CR>', opts)
keymap('n', '<leader>q', '<cmd>quit<CR>', opts)
keymap('n', '<leader>QQ', '<cmd>q!<CR>', opts)
keymap('n', '<leader>wq', '<cmd>wq<CR>', opts)

-- UI Toggles
keymap('n', '<leader>r', '<cmd>set wrap!<CR>', opts)
keymap('n', '<leader>n', '<cmd>set number! relativenumber!<CR>', opts)

-- File Explorer (Oil)
keymap('n', '-', function() require('oil').open() end, { desc = 'Open parent directory in Oil' })
keymap('n', '<leader>e', function() require('oil').open() end, { desc = 'Open parent directory in Oil' })

-- Telescope Keymaps
local builtin = require('telescope.builtin')
keymap('n', '<leader>ff', builtin.find_files, { desc = 'Find files' })
keymap('n', '<leader>fg', builtin.live_grep, { desc = 'Live grep search' })
keymap('n', '<leader>fb', builtin.buffers, { desc = 'Find open buffers' })
keymap('n', '<leader>fh', builtin.help_tags, { desc = 'Find help tags' })
