PLUGIN_DIR="$HOME/.local/share/zsh/plugins"
PLUGIN_LOCK="$PLUGIN_DIR/.plugins.lock"
[ -d "$PLUGIN_DIR" ] || mkdir -p "$PLUGIN_DIR"

# Reinstall function
update-plugins() {
  rm -fr "$PLUGIN_DIR"
  echo "Plugin lock removed. Restart your shell to reinstall plugins."
}

# Define plugins: "github_repo name"
plugins=(
  "romkatv/zsh-defer"
  "zsh-users/zsh-autosuggestions"
  "hlissner/zsh-autopair"
  "zsh-users/zsh-history-substring-search"
  "Aloxaf/fzf-tab"
  "junegunn/fzf"
  "subnixr/minimal"
  "zap-zsh/vim"
  "zsh-users/zsh-syntax-highlighting"
)

# Only install plugins if lock file doesn't exist
if [ ! -f "$PLUGIN_LOCK" ]; then
  for plugin in "${plugins[@]}"; do
    plugin_name=${plugin##*/}
    if [ ! -d "$PLUGIN_DIR/$plugin_name" ]; then
      echo "Installing $plugin_name"
      git clone --quiet "https://github.com/$plugin" "$PLUGIN_DIR/$plugin_name" --depth=1
    fi
  done
  date > "$PLUGIN_LOCK"
fi

# Source plugins
source "$PLUGIN_DIR/zsh-defer/zsh-defer.plugin.zsh"
source "$PLUGIN_DIR/zsh-autosuggestions/zsh-autosuggestions.zsh"
source "$PLUGIN_DIR/zsh-autopair/autopair.zsh"
source "$PLUGIN_DIR/zsh-history-substring-search/zsh-history-substring-search.zsh"
source "$PLUGIN_DIR/fzf-tab/fzf-tab.plugin.zsh"
source "$PLUGIN_DIR/fzf/shell/completion.zsh"
source "$PLUGIN_DIR/fzf/shell/key-bindings.zsh"
source "$PLUGIN_DIR/minimal/minimal.zsh"
source "$PLUGIN_DIR/vim/vim.plugin.zsh"

# Source config files
source "$ZDOTDIR/opts.zsh"
source "$ZDOTDIR/rose-pine.sh"
source "$ZDOTDIR/maintenance.zsh"

# Defer loading
zsh-defer source "$PLUGIN_DIR/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
for config in mise.zsh tmux.zsh functions.zsh alias.zsh keymap.zsh path.zsh pnpm.zsh opts.zsh; do
  zsh-defer source "$ZDOTDIR/$config"
done
