#!/usr/bin/env bash
#
# c3t — launcher for the club3090 test console TUI
#
# Runs the tool from its own isolated env (uv run / .venv), never the system Python.
#
set -euo pipefail

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
TOOL_DIR="${SCRIPT_DIR}/../tools/test-console"

# Ensure we're in the repo root for cwd resolution
REPO_ROOT="${SCRIPT_DIR}/.."

if [[ ! -d "${TOOL_DIR}" ]]; then
    echo "Error: test-console tool dir not found at ${TOOL_DIR}" >&2
    exit 1
fi

cd "${REPO_ROOT}"

# Prefer uv if available, fall back to .venv, fall back to pipx
if command -v uv &>/dev/null; then
    exec uv run --project "${TOOL_DIR}" -- python -m club3090_test_console "$@"
elif [[ -d "${TOOL_DIR}/.venv" ]]; then
    exec "${TOOL_DIR}/.venv/bin/python" -m club3090_test_console "$@"
elif command -v pipx &>/dev/null; then
    exec pipx run --spec "${TOOL_DIR}" c3t "$@"
else
    echo "Error: No Python runner found. Install uv, create a .venv, or install pipx." >&2
    echo "  Quick setup: cd ${TOOL_DIR} && python3 -m venv .venv && .venv/bin/pip install -e ." >&2
    exit 1
fi
