#!/bin/sh

set -eu

ORANGE="\033[33m"
RESET="\033[0m"

log() {
  printf "%b\n" "$1"
}

install_mise() {
  curl https://mise.run | sh
  echo "✅ Installed ${ORANGE}mise$RESET"
}

maybe_install_mise() {
  if ! command -v mise >/dev/null; then
    install_mise
  fi
}

# i don't care that much if intermediate dev branches commits aren't always conventional
# but i do care about convenional commits on main because there are used for semantic versioning/releasing
use_convco_as_git_editor_only_on_main() {
  # Create a wrapper script that decides which editor to use
  cat >.git/hooks/smart-commit-editor <<'EOF'
#!/bin/sh
# Smart editor wrapper that uses convco on default branch, normal editor otherwise

# Check if we're in a rebase
if [ -d "$(git rev-parse --git-dir)/rebase-merge" ] || [ -d "$(git rev-parse --git-dir)/rebase-apply" ]; then
  # In rebase, use normal editor
  default_editor=$(git config --global --get core.editor || echo "$EDITOR" || command -v nvim || command -v vim || command -v vi || command -v nano)
  exec $default_editor "$@"
fi

# Determine default branch
default_branch=$(git config --get init.defaultBranch || echo "main")

# Check current branch
if ! git rev-parse HEAD >/dev/null 2>&1; then
  # No commits yet, use default branch name
  current_branch=$(git symbolic-ref --short HEAD 2>/dev/null || echo "$default_branch")
else
  current_branch=$(git rev-parse --abbrev-ref HEAD)
fi

if [ "$current_branch" = "$default_branch" ]; then
  # Use convco on default branch
  exec convco commit "$@"
else
  # Use normal editor on other branches
  default_editor=$(git config --global --get core.editor || echo "$EDITOR" || command -v nvim || command -v vim || command -v vi || command -v nano)
  exec $default_editor "$@"
fi
EOF

  chmod +x .git/hooks/smart-commit-editor

  # Set this wrapper as the git editor (absolute path for worktree compatibility)
  git config --local core.editor "$(realpath .git/hooks/smart-commit-editor)"

  # Ensure sequence.editor is set for rebase operations to bypass our wrapper
  default_editor=$(git config --global --get core.editor || echo "$EDITOR" || command -v nvim || command -v vim || command -v vi || command -v nano)
  if [ -n "$default_editor" ]; then
    git config --local sequence.editor "$default_editor"
  fi

  echo "Configured smart commit editor to use convco on default branch only"
}

main() {
  maybe_install_mise
  mise up --bump
  log "To see available tasks: ${ORANGE}mise run$RESET"
  use_convco_as_git_editor_only_on_main
  hk install
}

main
