#!/bin/bash

# shellcheck shell=bash

# List tmux sessions
bridge_list_sessions() {
  if [[ -z "${1:-}" ]]; then
    json_error "Slug parameter required"
    return 1
  fi
  slug="$1"
  # Optional second parameter for worktree path
  worktree_path="${2:-}"
  
  if tmux_available; then
    if [[ -n "$worktree_path" ]]; then
      sessions_output="$(tmux_list_sessions_for_slug "$slug" "$worktree_path")"
    else
      sessions_output="$(tmux_list_sessions_for_slug "$slug")"
    fi
    if [[ -n "$sessions_output" ]]; then
      json_array_from_lines "$(printf '%s\n' "$sessions_output")"
    else
      echo "null"
    fi
  else
    echo "null"
  fi
}

# Create tmux session
bridge_create_session() {
  if [[ -z "${1:-}" ]]; then
    json_error "Slug required"
    return 1
  fi
  slug="$1"
  name="${2:-}"
  path="${3:-$(pwd)}"
  
  if tmux_available; then
    session_name="$(tmux_ensure_session "$slug" "$name" "$path")"
    echo "\"$session_name\""
  else
    json_error "tmux not available"
  fi
}

# Create exact tmux session
bridge_create_session_exact() {
  if [[ -z "${1:-}" ]]; then
    json_error "Session name required"
    return 1
  fi
  session_name="$1"
  path="${2:-$(pwd)}"
  if tmux_available; then
    created="$(tmux_create_session "$session_name" "$path")" || { json_error "Failed to create exact session"; return 1; }
    echo "\"$created\""
  else
    json_error "tmux not available"
  fi
}

# Kill tmux session
bridge_kill_session() {
  if [[ -z "${1:-}" ]]; then
    json_error "Session name required"
    return 1
  fi
  session_name="$1"
  
  if tmux_available; then
    if tmux_kill_session "$session_name"; then
      echo "true"
    else
      json_error "Failed to kill session"
    fi
  else
    json_error "tmux not available"
  fi
}

# Attach to tmux session
bridge_attach_session() {
  if [[ -z "${1:-}" ]]; then
    json_error "Session name required"
    return 1
  fi
  session_name="$1"
  
  if tmux_available; then
    tmux_attach_session "$session_name"
    echo "true"
  else
    json_error "tmux not available"
  fi
}

# Send keys to tmux session
bridge_tmux_send_keys() {
  if [[ -z "${1:-}" ]]; then
    json_error "Session name required"
    return 1
  fi
  session_name="$1"; shift || true
  if tmux_available; then
    tmux_send_keys "$session_name" "$*" || { json_error "Failed to send keys"; return 1; }
    echo '{"ok":true}'
  else
    json_error "tmux not available"
  fi
}

# Cycle to next/previous registered Orchestra session in the current workspace
bridge_cycle_workspace_session() {
  if [[ -z "${1:-}" ]] || [[ -z "${2:-}" ]]; then
    json_error "Session name and direction required"
    return 1
  fi
  session_name="$1"
  direction="$2"
  target_client="${3:-}"

  if tmux_available; then
    tmux_cycle_workspace_session "$session_name" "$direction" "$target_client" || {
      json_error "Failed to cycle workspace session"
      return 1
    }
    echo '{"ok":true}'
  else
    json_error "tmux not available"
  fi
}

# Show Orchestra tmux shortcut help popup
bridge_tmux_help_popup() {
  target_client="${1:-}"

  if tmux_available; then
    tmux_show_orchestra_help_popup "$target_client" || {
      json_error "Failed to show tmux help popup"
      return 1
    }
    echo '{"ok":true}'
  else
    json_error "tmux not available"
  fi
}

# Get session metadata
bridge_session_metadata() {
  if [[ -z "${1:-}" ]]; then
    json_error "Session name required"
    return 1
  fi
  session_name="$1"
  
  if tmux_available; then
    # Get tmux metadata for context detection
    window_name=$(tmux display-message -t "$session_name" -p '#{window_name}' 2>/dev/null || echo "")
    pane_title=$(tmux display-message -t "$session_name" -p '#{pane_title}' 2>/dev/null || echo "")
    pane_cmd=$(tmux display-message -t "$session_name" -p '#{pane_current_command}' 2>/dev/null || echo "")
    pane_mode=$(tmux display-message -t "$session_name" -p '#{pane_mode}' 2>/dev/null || echo "")
    alternate_on=$(tmux display-message -t "$session_name" -p '#{alternate_on}' 2>/dev/null || echo "0")
    mouse_any_flag=$(tmux display-message -t "$session_name" -p '#{mouse_any_flag}' 2>/dev/null || echo "0")
    auto_rename_requested=$(tmux show-options -qv -t "$session_name" @orchestra_auto_rename_requested 2>/dev/null || echo "")
    case "$(printf '%s' "$alternate_on" | tr '[:upper:]' '[:lower:]')" in
      1|yes|true|on) alternate_on=true ;;
      *) alternate_on=false ;;
    esac
    case "$(printf '%s' "$mouse_any_flag" | tr '[:upper:]' '[:lower:]')" in
      1|yes|true|on) mouse_any_flag=true ;;
      *) mouse_any_flag=false ;;
    esac
    case "$(printf '%s' "$auto_rename_requested" | tr '[:upper:]' '[:lower:]')" in
      1|yes|true|on) auto_rename_requested=true ;;
      *) auto_rename_requested=false ;;
    esac

    # Return JSON with metadata
    json_object \
      "window_name:s=$window_name" \
      "pane_title:s=$pane_title" \
      "current_command:s=$pane_cmd" \
      "pane_mode:s=$pane_mode" \
      "alternate_on:b=$alternate_on" \
      "mouse_any_flag:b=$mouse_any_flag" \
      "auto_rename_requested:b=$auto_rename_requested"
  else
    json_error "tmux not available"
  fi
}

# Get session preview
bridge_session_preview() {
  if [[ -z "${1:-}" ]]; then
    json_error "Session name required"
    return 1
  fi
  session_name="$1"
  
  if tmux_available; then
    preview="$(tmux_session_preview "$session_name")"
    json_stringify "$preview"
  else
    echo "\"tmux not available\""
  fi
}

# Check if tmux is available
bridge_tmux_available() {
  if tmux_available; then
    echo "true"
  else
    echo "false"
  fi
}
