#!/usr/bin/env bash
# ===========================================================================
# polymath — Integration test CLI for the Polymathera multi-agent framework
# ===========================================================================
#
# This script launches the polymath CLI tool, which runs complete analysis
# workflows on codebases using the Colony multi-agent framework.
#
# Prerequisites:
#   - Python 3.10+ with the colony package installed
#   - typer, rich (pip install typer rich)
#   - pyyaml for YAML config support (pip install pyyaml)
#   - A running Ray cluster for actual agent execution
#
# Usage:
#   # Run an integration test
#   export PYTHONPATH=$WORKSPACE/polymathera_inc/colony/python:$PYTHONPATH
#   ./polymath run /path/to/codebase --analysis impact --analysis compliance
#
#   # Run with a YAML config file
#   ./polymath run /path/to/codebase --config test_config.yaml
#
#   # Show planned hierarchy without running (dry run)
#   ./polymath run /path/to/codebase --analysis impact --dry-run
#
#   # List available analyses, agents, and capabilities
#   ./polymath list analyses
#   ./polymath list agents
#   ./polymath list capabilities
#
#   # Generate a sample YAML config
#   ./polymath init-config --output my_test.yaml
#
#   # Describe a specific analysis type
#   ./polymath describe impact
#
# Environment Variables:
#   RAY_ADDRESS        — Ray cluster address (default: auto-detect)
#   POLYMATH_APP_NAME  — Polymathera serving application name
#   POLYMATH_LOG_LEVEL — Logging level (DEBUG, INFO, WARNING, ERROR)
#
# ===========================================================================

set -euo pipefail

# --- Resolve script location ---
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# --- Configure logging ---
if [ -n "${POLYMATH_LOG_LEVEL:-}" ]; then
    export LOG_LEVEL="${POLYMATH_LOG_LEVEL}"
fi

# --- Pass through RAY_ADDRESS if set ---
if [ -n "${RAY_ADDRESS:-}" ]; then
    export RAY_ADDRESS
fi

# --- Pass through app name if set ---
EXTRA_ARGS=()
if [ -n "${POLYMATH_APP_NAME:-}" ]; then
    EXTRA_ARGS+=("--app-name" "${POLYMATH_APP_NAME}")
fi

# --- Find Python ---
# Prefer the Python from the current virtual environment, fall back to system.
if [ -n "${VIRTUAL_ENV:-}" ]; then
    PYTHON="${VIRTUAL_ENV}/bin/python"
elif command -v python3 &> /dev/null; then
    PYTHON="python3"
elif command -v python &> /dev/null; then
    PYTHON="python"
else
    echo "Error: Python not found. Please install Python 3.10+ or activate a virtual environment." >&2
    exit 1
fi

# --- Verify minimum Python version ---
PY_VERSION=$("${PYTHON}" -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")' 2>/dev/null || echo "0.0")
PY_MAJOR=$(echo "${PY_VERSION}" | cut -d. -f1)
PY_MINOR=$(echo "${PY_VERSION}" | cut -d. -f2)

if [ "${PY_MAJOR}" -lt 3 ] || { [ "${PY_MAJOR}" -eq 3 ] && [ "${PY_MINOR}" -lt 10 ]; }; then
    echo "Error: Python 3.10+ is required (found ${PY_VERSION})." >&2
    exit 1
fi

# --- Run the CLI ---
# We invoke polymath.py directly rather than `python -m colony.cli.polymath`
# because the latter triggers colony/__init__.py which eagerly imports vllm/CUDA
# dependencies. The polymath CLI only needs those heavy imports when actually
# connecting to a Ray cluster (inside run_integration_test), not for listing
# analyses, generating configs, or dry-run mode.
exec "${PYTHON}" "${SCRIPT_DIR}/polymath.py" "${EXTRA_ARGS[@]}" "$@"
