#!/usr/bin/env bash
# Secondary sesh switch/discovery picker.
# If the picker is dismissed with no selection and tmux exposes a current pane
# directory, fall back to tmproj so session entry stays aligned with the
# canonical current-project attach/create flow.

resolve_bin() {
  local name="$1"
  shift

  if command -v "$name" >/dev/null 2>&1; then
    command -v "$name"
    return 0
  fi

  local candidate
  for candidate in "$@"; do
    [[ -n "$candidate" && -x "$candidate" ]] && {
      printf '%s\n' "$candidate"
      return 0
    }
  done

  return 0
}

TMUX_BIN="${TMUX_BIN:-$(resolve_bin tmux "/opt/homebrew/bin/tmux" "/run/current-system/sw/bin/tmux" "$HOME/.nix-profile/bin/tmux")}"
BASH_BIN="${BASH_BIN:-$(resolve_bin bash "/bin/bash" "/usr/bin/bash" "$HOME/.nix-profile/bin/bash")}"
SESH_BIN="${SESH_BIN:-$(resolve_bin sesh "/opt/homebrew/bin/sesh" "/run/current-system/sw/bin/sesh" "$HOME/.nix-profile/bin/sesh")}"
FZF_TMUX_BIN="${FZF_TMUX_BIN:-$(resolve_bin fzf-tmux "/opt/homebrew/bin/fzf-tmux" "/run/current-system/sw/bin/fzf-tmux" "$HOME/.nix-profile/bin/fzf-tmux")}"
TMPROJ_BIN="${TMPROJ_BIN:-$(resolve_bin tmproj "/opt/homebrew/bin/tmproj" "/run/current-system/sw/bin/tmproj" "$HOME/.nix-profile/bin/tmproj")}"
SCRIPT_DIR=${TMUX_HOME:-$HOME/.config/tmux}
CUR_SESS=""
CUR_DIR=""
if [[ -n "$TMUX_BIN" ]]; then
  CUR_SESS=$("$TMUX_BIN" display-message -p '#S' 2>/dev/null || true)
  CUR_DIR=$("$TMUX_BIN" display-message -p '#{pane_current_path}' 2>/dev/null || true)
fi
CUR_DIR_SHORT=${CUR_DIR/#$HOME/\~}

tmux_message() {
  local message="$1"
  if [[ -n "$TMUX_BIN" ]]; then
    "$TMUX_BIN" display-message "$message"
  else
    printf '%s\n' "$message" >&2
  fi
}

[[ -n "$SESH_BIN" ]] || {
  tmux_message "sesh-picker: sesh not found"
  exit 1
}

[[ -n "$FZF_TMUX_BIN" ]] || {
  tmux_message "sesh-picker: fzf-tmux not found"
  exit 1
}

export SESH_BIN

SESSION=$(
  "$BASH_BIN" "$SCRIPT_DIR/sesh-all.sh" | "$FZF_TMUX_BIN" -p 80%,70% \
    --ansi \
    --no-sort \
    --border-label " sesh " \
    --prompt "> " \
    --header "^a all  ^t tmux  ^c configs  ^x zoxide  ^d kill" \
    --bind "tab:down,btab:up" \
    --bind "ctrl-a:change-prompt(> )+reload($BASH_BIN $SCRIPT_DIR/sesh-all.sh)" \
    --bind "ctrl-t:change-prompt(tmux> )+reload($SESH_BIN list -t | grep -vxF '$CUR_SESS' | awk '{printf \"\\033[35m■\\033[0m %s\\n\", \$0}')" \
    --bind "ctrl-c:change-prompt(configs> )+reload($SESH_BIN list -c | grep -vxF '$CUR_SESS' | awk '{printf \"\\033[33m□\\033[0m %s\\n\", \$0}')" \
    --bind "ctrl-x:change-prompt(zoxide> )+reload($BASH_BIN $SCRIPT_DIR/zoxide-list.sh | grep -vxF '$CUR_DIR_SHORT' | awk '{printf \"\\033[34m›\\033[0m %s\\n\", \$0}')" \
    --bind "ctrl-d:execute(tmux kill-session -t {2..})+change-prompt(> )+reload($BASH_BIN $SCRIPT_DIR/sesh-all.sh)"
)

# Strip ANSI escape codes, then the symbol prefix
strip_ansi() {
  local input="$1"
  local output=""
  local ch=""

  while [[ -n "$input" ]]; do
    ch=${input:0:1}
    if [[ "$ch" == $'\e' && ${input:1:1} == '[' ]]; then
      input=${input:2}
      while [[ -n "$input" ]]; do
        ch=${input:0:1}
        input=${input:1}
        [[ "$ch" == 'm' ]] && break
      done
      continue
    fi

    output+="$ch"
    input=${input:1}
  done

  printf '%s' "$output"
}

SESSION=$(strip_ansi "$SESSION")
SESSION=${SESSION#■ }
SESSION=${SESSION#□ }
SESSION=${SESSION#› }

if [[ -n "$SESSION" ]]; then
  exec "$SESH_BIN" connect "$SESSION"
fi

if [[ -n "$CUR_DIR" ]]; then
  if [[ -n "$TMPROJ_BIN" ]]; then
    exec "$TMPROJ_BIN" "$CUR_DIR"
  fi

  tmux_message "sesh-picker: tmproj not found for current-project fallback"
fi

exit 0
