-- WARNING: neorg does some pretty stupid stuff when it comes to the plugins it
-- wants (using luarocks), in order to get around all that bullshit I've
-- manually added it's dependencies. Because I don't want this to randomly break
-- I've also pinned neorg to the latest working version that I've messed around
-- with.
--
-- NOTE: for my future self to update this thingy while not breaking
-- dependencies take a look at the build.lua for versioning info. Also make sure
-- to check the release notes on github for info on breaking changes.

local workspace_cache = vim.fs.joinpath(vim.fn.stdpath("data"), "neorg-workspace-cache.lua")

--- populate neorg workspaces from path or cache
---@param path string path to populate workspaces from
---@param cache boolean? if true will re populate the cache from the fs
---@return table workspaces
local function populate_workspaces(path, cache)
  local workspaces = {}
  cache = cache or false

  if vim.fn.filereadable(workspace_cache) == 1 and not cache then
    local ok, ret = pcall(dofile, workspace_cache)
    if ok and type(ret) == "table" then
      return ret
    end
  end

  -- loop through all files in path if path is not empty
  if vim.fn.empty(vim.fn.glob(path)) == 0 then
    for n, t in vim.fs.dir(path) do
      if string.sub(n, 1, 1) == "." or (t ~= "directory" and t ~= "link") then
        goto continue
      end
      -- make sure this still works if the last charachter in the path isn't a '/'
      workspaces[n] = (string.sub(path, #path, #path) == "/") and path..n or path.."/"..n
      ::continue::
    end
  end

  -- write data to cachefile
  local f = io.open(workspace_cache, "w+")
  if not f then
    return workspaces
  end

  f:write("return")
  local ret = vim.inspect(workspaces)
  if type(ret) == "string" then
    f:write(ret)
  else
    f:write("false")
  end

  f:close()

  return workspaces
end

return { 'nvim-neorg/neorg',
  disable = vim.version.lt(vim.version(), { 0, 10, 0 }),
  branch = 'v9.1.1',
  requires = {
    'nvim-lua/plenary.nvim',
    'nvim-treesitter/nvim-treesitter',
    'folke/zen-mode.nvim',
    'hrsh7th/nvim-cmp',
    { 'nvim-neorg/neorg-telescope',
      requires = 'nvim-telescope/telescope.nvim'
    },

    -- NOTE: these are usually installed by neorg via luarocks, the versions
    -- were picked based on the build.lua found in the root of the neorg repo
    { 'nvim-neotest/nvim-nio',
      branch = 'v1.7.0'
    },
    { 'nvim-neorg/lua-utils.nvim',
      branch = 'v1.0.2'
    },
    { 'MunifTanjim/nui.nvim',
      branch = '0.3.0'
    },
    { 'pysan3/pathlib.nvim',
      branch = 'v2.2.2'
    }
  },

  function()
    local wsphome = (os.getenv("XDG_DOCUMENTS_DIR") or
      (os.getenv("HOME").."/Documents")).."/notes/"

    require('neorg').setup {
      load = {
        -- not sure how to sort the modules so ima just put the empty ones first
        ["core.defaults"] = {},
        ["core.export"] = {},
        ["core.export.markdown"] = {},
        ["core.integrations.telescope"] = {},
        ["core.summary"] = {},
        ["core.ui.calendar"] = {},

        ["core.completion"] = {
          config = {
            engine = "nvim-cmp"
          }
        },
        ["core.concealer"] = {
          config = {
            folds = false
          }
        },
        ["core.dirman"] = {
          config = {
            -- a list of available workspaces are generated at runtime >:)
            workspaces = populate_workspaces(wsphome)
          }
        },
        ["core.esupports.metagen"] = {
          config = {
            type = "auto"
          }
        },
        ["core.presenter"] = {
          config = {
            zen_mode = "zen-mode"
          }
        },
        ["core.qol.toc"] = {
          config = {
            close_after_use = true
          }
        },
        ["core.tangle"] = {
          config = {
            tangle_on_write = true
          }
        }
      }
    }
  end
}