-- completions.lua: Provides intelligent code completion via LSP or snippet engines.

-- completion.lua  (nvim-cmp + LuaSnip + TabNine  –  no unzip required)
return {
  ------------------------------------------------------------------
  -- 1. Snippet engine & pre-made snippets
  ------------------------------------------------------------------
  {
    "L3MON4D3/LuaSnip",
    version = "v2.*",
    build = "make install_jsregexp",
    dependencies = { "rafamadriz/friendly-snippets" }
  },

  ------------------------------------------------------------------
  -- 2. Core completion engine + all standard sources
  ------------------------------------------------------------------
  {
    "hrsh7th/nvim-cmp",
    dependencies = {
      "hrsh7th/cmp-nvim-lsp",
      "hrsh7th/cmp-buffer",
      "hrsh7th/cmp-path",
      "hrsh7th/cmp-cmdline",
      "hrsh7th/cmp-nvim-lua",

      ------------------------------------------------------------------
      -- 3. TabNine (AI) source  –  install without unzip
      ------------------------------------------------------------------
      {
        "tzachar/cmp-tabnine",
        -- download & unpack with curl + tar (gzip) only
        build = function()
          local api = vim.fn
          local version = api.systemlist('curl -sS https://update.tabnine.com/bundles/version')[1]
          local platform = (function()
            local os = api.substitute(api.system('uname -s'), '\n', '', '')
            local arch = api.substitute(api.system('uname -m'), '\n', '', '')
            return arch ..
            '-unknown-' .. (os == 'Linux' and 'linux-musl' or os == 'Darwin' and 'apple-darwin' or 'linux-musl')
          end)()
          local root = api.stdpath('data') .. '/lazy/cmp-tabnine'
          local target = root .. '/binaries/' .. version .. '/' .. platform
          api.mkdir(target, 'p')
          api.system(string.format(
            'curl -L https://update.tabnine.com/bundles/%s/%s/TabNine.zip | gzip -dc | tar -C %s -xf -',
            version, platform, target))
          api.system('chmod +x ' .. target .. '/TabNine')
          vim.g.cmp_tabnine_binary = target .. '/TabNine'
        end,
        opts = {
          max_lines = 1000,
          max_num_results = 20,
          sort = true,
          run_on_every_keystroke = true,
          show_prediction_strength = false,
        },
        config = function(_, opts)
          require('cmp_tabnine.config'):setup(opts)
        end,
      },
    },

    ------------------------------------------------------------------
    -- 4. nvim-cmp setup
    ------------------------------------------------------------------
    config = function()
      local cmp = require('cmp')
      require('luasnip.loaders.from_vscode').lazy_load()

      cmp.setup({
        snippet = {
          expand = function(args)
            require('luasnip').lsp_expand(args.body)
          end,
        },
        window = {
          completion = cmp.config.window.bordered(),
          documentation = cmp.config.window.bordered(),
        },
        mapping = cmp.mapping.preset.insert({
          ['<Tab>']   = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
          ['<S-Tab>'] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),
          ['<CR>']    = cmp.mapping.confirm({ select = true }),
          ['<C-e>']   = cmp.mapping.abort(),
          ['<C-b>']   = cmp.mapping.scroll_docs(-4),
          ['<C-f>']   = cmp.mapping.scroll_docs(4),
        }),
        sources = cmp.config.sources({
          { name = 'cmp_tabnine', priority = 100 },
          { name = 'nvim_lsp' },
          { name = 'luasnip' },
          { name = 'nvim_lua' },
        }, {
          { name = 'buffer' },
          { name = 'path' },
        }),
        formatting = {
          format = function(entry, item)
            if entry.source.name == 'cmp_tabnine' then item.menu = '' end
            return item
          end,
        },
      })

      -- cmd-line completion
      cmp.setup.cmdline({ '/', '?' }, { sources = { { name = 'buffer' } } })
      cmp.setup.cmdline(':', { sources = cmp.config.sources({ { name = 'path' } }, { { name = 'cmdline' } }) })
    end,
  },
}
