local fs = require("functions.fs")
local git = require("functions.git")

vim.api.nvim_create_user_command("CopyToPlayers", function()
  local CONFIGFILE = ".dsa-players-path"

  local pathFile = vim.fs.joinpath(vim.fn.getcwd(), CONFIGFILE)
  local fileExists = vim.fn.empty(vim.fn.glob(pathFile)) == 0
  if not fileExists then
    vim.notify("Can't find target path. Missing file", "error", {
      title = "Copy to players",
    })

    return
  end

  local targetPath = vim.fs.joinpath(vim.fn.getcwd(), table.concat(vim.fn.readfile(pathFile)))

  os.execute(string.format("mkdir -p '%s'", targetPath))

  local picker = Snacks.picker.get({ source = "explorer" })
  local localPath = picker[1]:current().file
  local targetFilePath = vim.fs.joinpath(targetPath, vim.fn.fnamemodify(localPath, ":t"))
  os.execute(string.format("ln -s '%s' '%s'", localPath, targetFilePath))

  git.commitFile(targetFilePath)
end, {})
