#!/bin/bash

###############################################################################
# gwr.sh – Git-Worktree Rust TUI launcher
# ---------------------------------------------------------------------------
# This script launches the Rust TUI application for git worktree and tmux
# session management. It automatically detects the built binary location
# and passes through any command-line arguments.
###############################################################################

# Recommended wrapper (place in ~/.bash_profile or ~/.zshrc):
#
# gwr () {
#   local script="/absolute/path/to/gwr.sh"  # UPDATE THIS PATH
#   local out
#   out="$(command bash "$script" "$@")"
#   local status=$?
#   local cd_line
#   cd_line="$(printf '%s\n' "$out" | grep -m1 '^cd \".*\"')"
#   [[ -n $cd_line ]] && eval "$cd_line"
#   printf '%s\n' "$out" | grep -v -F "$cd_line"
#   return $status
# }

set -o errexit
set -o pipefail
set -o nounset

# shellcheck shell=bash

###############################################################################
# gwr.sh – Git-Worktree Rust TUI launcher
###############################################################################

set -o errexit
set -o pipefail
set -o nounset

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/shell/gwr_load.sh"
gwr_load_core

# Handle help flag early
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
    gwr_show_usage
    exit 0
fi

# Handle update check flag
if [[ "${1:-}" == "--check-updates" || "${1:-}" == "--update" ]]; then
    "$SCRIPT_DIR/shell/gwr/check-updates.sh"
    exit $?
fi

# Check prerequisites
gwr_check_git_repo
gwr_check_bridge_script

# Find the binary
if ! BINARY_PATH=$(gwr_find_binary); then
    gwr_error "gw-tui binary not found!"
    gwr_error ""
    gwr_error "Please build the Rust TUI first:"
    gwr_error "  ./build.sh"
    gwr_error ""
    gwr_error "Or install globally:"
    gwr_error "  ./build.sh --install"
    gwr_error ""
    gwr_error "Binary search paths:"
    if [[ -n "${GW_TUI_BIN:-}" ]]; then
        gwr_error "  • $GW_TUI_BIN (from GW_TUI_BIN)"
    fi
    gwr_error "  • $SCRIPT_DIR/gw-tui/target/release/gw-tui"
    gwr_error "  • $SCRIPT_DIR/gw-tui/target/debug/gw-tui"
    gwr_error "  • gw-tui (in PATH)"
    exit 1
fi

BRIDGE_PATH="$SCRIPT_DIR/gw-bridge.sh"

gwr_set_terminal_title

# Check for updates silently (once per day)
gwr_check_updates_silent

exec "$BINARY_PATH" --bridge-path "$BRIDGE_PATH" "$@"

# Handle help flag early
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
    show_usage
    exit 0
fi

# Handle update check flag
if [[ "${1:-}" == "--check-updates" || "${1:-}" == "--update" ]]; then
    "$SCRIPT_DIR/shell/gwr/check-updates.sh"
    exit $?
fi

# Check prerequisites
check_git_repo
check_bridge_script

# Find the binary
if ! BINARY_PATH=$(find_gw_tui_binary); then
    error "gw-tui binary not found!"
    error ""
    error "Please build the Rust TUI first:"
    error "  ./build.sh"
    error ""
    error "Or install globally:"
    error "  ./build.sh --install"
    error ""
    error "Binary search paths:"
    if [[ -n "${GW_TUI_BIN:-}" ]]; then
        error "  • $GW_TUI_BIN (from GW_TUI_BIN)"
    fi
    error "  • $SCRIPT_DIR/gw-tui/target/release/gw-tui"
    error "  • $SCRIPT_DIR/gw-tui/target/debug/gw-tui"
    error "  • gw-tui (in PATH)"
    exit 1
fi

# Get bridge script path
BRIDGE_PATH="$SCRIPT_DIR/gw-bridge.sh"

set_terminal_title

# Run the Rust TUI with bridge path and all arguments passed through
exec "$BINARY_PATH" --bridge-path "$BRIDGE_PATH" "$@"
