-- Bootstrap packer.nvim if not already installed
local ensure_packer = function()
  local fn = vim.fn
  local install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"
  if fn.empty(fn.glob(install_path)) > 0 then
    fn.system({ "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path })
    vim.cmd([[packadd packer.nvim]])
    return true
  end
  return false
end

local packer_bootstrap = ensure_packer()

-- Load user configuration files
require("user.options")
require("user.keymaps")
require("user.cmp")
require("user.autopairs")
require("user.telescope")
require("user.gruvbox")
require("user.lsp")
require("user.typescript-tools")
require("user.null-ls")
require("user.treesitter")
require("user.terminal")
require("user.markdown")
require("user.autocmds")
require("user.files_utils")
require("user.commands")
require("user.comment")
require("user.trouble")
require("user.android")
require("user.flutter")

return require("packer").startup(function(use)
    -- Let packer manage itself
    use("wbthomason/packer.nvim")

    -- Completion framework
    use("hrsh7th/nvim-cmp")
    use("hrsh7th/cmp-buffer")
    use("hrsh7th/cmp-path")
    use("hrsh7th/cmp-nvim-lsp")
    use("hrsh7th/cmp-nvim-lua")
    use("saadparwaiz1/cmp_luasnip")
    use("L3MON4D3/LuaSnip")

    -- Load test plugin configuration
    require("user.test")(use)

    -- NERDTree plugin
    use("preservim/nerdtree")

    -- Plugin for integrating formatters/linters (community fork of archived null-ls)
    use("nvimtools/none-ls.nvim")

    -- Modern TypeScript integration (replaces archived nvim-lsp-ts-utils)
    use({
        "pmizio/typescript-tools.nvim",
        requires = { "nvim-lua/plenary.nvim", "neovim/nvim-lspconfig" },
    })

    -- Treesitter
    use("nvim-treesitter/nvim-treesitter")

    -- Auto pairs
    use("windwp/nvim-autopairs")

    -- LSP Configuration
    use("neovim/nvim-lspconfig")

    -- Java Language Server support
    use("mfussenegger/nvim-jdtls")

    -- Markdown Preview (optional)
    use({
      "iamcco/markdown-preview.nvim",
      run = function()
        vim.fn["mkdp#util#install"]()
      end,
    })

    use({
        "nvim-telescope/telescope.nvim",
        tag = "0.1.8",
        -- or                            , branch = '0.1.x',
        requires = { { "nvim-lua/plenary.nvim" } },
    })

    --  Comment
    use 'numToStr/Comment.nvim'

    -- Trouble: workspace diagnostics panel
    use {
        "folke/trouble.nvim",
        requires = { "nvim-tree/nvim-web-devicons" },
    }


    -- Telescope FZF native
    use({
        "nvim-telescope/telescope-fzf-native.nvim",
        run = "make",
    })

    use({ "ellisonleao/gruvbox.nvim" })

    use({
        "williamboman/mason.nvim",
        config = function()
            require("mason").setup()
        end,
    })

    -- Mason LSPConfig for integrating Mason with nvim-lspconfig
    use("williamboman/mason-lspconfig.nvim")

    -- Automatically install plugins on first run
    if packer_bootstrap then
        require("packer").sync()
    end
end)
