-- vimtex.lua: Full LaTeX editing support (compiling, viewing, and syntax).

return {
  "lervag/vimtex",
  ft = "tex",
  config = function()
    -- PDF viewer
    vim.g.vimtex_view_method = "zathura"
    -- Compiler settings
    vim.g.vimtex_compiler_latexmk = {
      continuous = 1,
      callback = 1,
      options = {
        "-pdf",
        "-shell-escape",
        "-verbose",
        "-file-line-error",
        "-synctex=1",
        "-interaction=nonstopmode",
      },
    }
    -- Concealment (makes LaTeX prettier to read)
    vim.g.vimtex_syntax_conceal = {
      accents = 1,
      math_symbols = 1,
      math_fracs = 1,
      math_super_sub = 1,
    }
    vim.g.vimtex_toc_config = {
      name = "TOC",
      layers = { "content", "todo", "include" },
      split_width = 25,
      todo_sorted = 0,
      show_help = 1,
      show_numbers = 1,
    }
    -- Don't open quickfix window automatically
    vim.g.vimtex_quickfix_mode = 0
    -- Enable folding
    vim.g.vimtex_fold_enabled = 1
    -- What to fold (customize as needed)
    vim.g.vimtex_fold_types = {
      envs = {
        enabled = 1,
        whitelist = {}, -- Empty = all environments
        blacklist = {},
      },
      sections = {
        enabled = 1,
        sections = {
          "%(add)?part",
          "%(chapter|%(sub)*section|%(sub)?paragraph)",
          "appendix",
        },
      },
      items = {
        enabled = 0, -- Don't fold individual list items
      },
    }
    -- Disable some features for better performance (optional)
    vim.g.vimtex_indent_enabled = 1
    vim.g.vimtex_syntax_enabled = 1

    -- Enhanced concealment settings - ADD THESE OPTIONS
    vim.g.vimtex_syntax_conceal = {
      accents = 1,
      ligatures = 1,
      cites = 1,
      fancy = 1,
      spacing = 1,
      greek = 1,
      math_bounds = 1,
      math_delimiters = 1,
      math_fracs = 1,
      math_super_sub = 1,
      math_symbols = 1,
      sections = 0,
      styles = 1,
    }

    -- Set conceal level (2 = moderate concealment)
    vim.opt.conceallevel = 2

    -- KEYBINDS FOR UNFOLDING AND CONCEALMENT TOGGLE
    vim.api.nvim_create_autocmd("FileType", {
      pattern = "tex",
      callback = function()
        -- Toggle all folds open/close
        vim.keymap.set("n", "<leader>fo", "zR", { buffer = true, desc = "Open all folds" })
        vim.keymap.set("n", "<leader>fc", "zM", { buffer = true, desc = "Close all folds" })

        -- Toggle concealment (show/hide LaTeX commands)
        vim.keymap.set("n", "<leader>tc", function()
          if vim.opt.conceallevel:get() == 0 then
            vim.opt.conceallevel = 2
            print("Concealment enabled")
          else
            vim.opt.conceallevel = 0
            print("Concealment disabled")
          end
        end, { buffer = true, desc = "Toggle concealment" })

        -- Alternative: Quick toggle for specific vimtex concealment
        vim.keymap.set("n", "<leader>tv", function()
          if vim.g.vimtex_syntax_conceal_enabled == 1 then
            vim.g.vimtex_syntax_conceal_enabled = 0
            print("VimTeX concealment disabled")
          else
            vim.g.vimtex_syntax_conceal_enabled = 1
            print("VimTeX concealment enabled")
          end
        end, { buffer = true, desc = "Toggle vimtex conceal" })
      end,
    })

    -- -- You can also add global keybinds if preferred
    -- vim.keymap.set("n", "<leader>lt", "<cmd>VimtexTocToggle<cr>", { desc = "LaTeX TOC" })
    -- vim.keymap.set("n", "<leader>lc", "<cmd>VimtexCompile<cr>", { desc = "LaTeX compile" })
    -- vim.keymap.set("n", "<leader>lv", "<cmd>VimtexView<cr>", { desc = "LaTeX view" })
  end,
}
