#!/usr/bin/env bash
# wms - workmux with sessions (not windows)
# Workaround for https://github.com/raine/workmux/issues/42

set -euo pipefail

usage() {
  echo "Usage: wms <branch> [workmux add options]"
  echo ""
  echo "Creates a new tmux session per worktree (instead of windows)"
  echo ""
  echo "Options passed to 'workmux add' (e.g., -A for auto-name, -P for prompt file)"
  exit 1
}

bin_script() {
  local script_name="$1"
  local script_path="${DOTFILES_BIN:-$HOME/.config/dotfiles/bin}/$script_name"
  [[ -x "$script_path" ]] || script_path="$HOME/.config/dotfiles/bin/$script_name"
  [[ -x "$script_path" ]] || return 1
  printf '%s\n' "$script_path"
}

if [[ $# -lt 1 ]] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
  usage
fi

branch="$1"
shift

# Create worktree without switching (-b = background)
workmux add -b "$branch" "$@" || exit 1

# Get the worktree path
wt_path=$(workmux path "$branch" 2>/dev/null)
if [[ -z "$wt_path" ]]; then
  echo "Failed to get worktree path for $branch" >&2
  exit 1
fi

if project_name_helper=$(bin_script tmux-project-name); then
  session_name=$($project_name_helper "$wt_path")
else
  session_name=$(basename -- "$wt_path" | sed -E 's/[^[:alnum:]_-]+/-/g; s/^-+//; s/-+$//; s/-+/-/g')
fi

if tmux has-session -t "$session_name" 2>/dev/null; then
  echo "Session '$session_name' already exists, switching to it"
  tmux switch-client -t "$session_name"
else
  # Create new session, detached first
  tmux new-session -d -s "$session_name" -c "$wt_path"
  # Run the pane layout setup (from workmux config)
  tmux send-keys -t "$session_name" "workmux open '$branch'" Enter
  # Switch to the new session
  tmux switch-client -t "$session_name"
fi
