#!/usr/bin/env bash

# band-aider: load the local Band-Aider environment and launch the CLI.
#
# Examples:
#   scripts/band-aider --repo-root /workspace/project --src-root src \
#     --tests-root tests --task "Fix the issue"
#   scripts/band-aider --resume --repo-container worker --repo-root /testbed \
#     --src-root package --tests-root tests
#
# The final exec is the only application subprocess: it preserves the exact
# CLI exit status. Optional Model2Vec checks run before it to select an
# accelerator or explain the full-file-parser fallback.

set -u

usage() {
  cat <<'USAGE'
Usage: scripts/band-aider --task "goal" [--src-root src] [--tests-root tests] [band-aider args...]
       scripts/band-aider --resume

Loads Band-Aider environment settings, then runs the current SupremeLoop CLI.
USAGE
}

if [ "${1:-}" = "--help" ] || [ "${1:-}" = "-h" ]; then
  usage
  exit 0
fi

ENV_FILE="${BAND_AIDER_ENV_FILE:-band-aider.env}"

# Load optional environment configuration only when the configured file exists.
if [ -f "$ENV_FILE" ]; then
  set -a
  # shellcheck disable=SC1090
  . "$ENV_FILE"
  set +a
fi

BAND_AIDER_TOKEN_OVERFLOW_STRATEGY="${BAND_AIDER_TOKEN_OVERFLOW_STRATEGY:-miser}"
PYTHON_BIN="${BAND_AIDER_PYTHON_BIN:-python3}"

# Verify the selected interpreter before any optional capability checks.
if ! command -v "$PYTHON_BIN" >/dev/null 2>&1; then
  echo "Python interpreter not found: $PYTHON_BIN"
  exit 1
fi

# The two branches below select Model2Vec when installed and cached; otherwise
# they deliberately retain the supported full-file parsing behavior.
if ! "$PYTHON_BIN" -c '
from band_aider.judgement.model2vec_code_parser import model2vec_package_available
import sys

sys.exit(0 if model2vec_package_available() else 1)
' >/dev/null 2>&1; then
  echo "model2vec is not installed for $PYTHON_BIN."
  echo "Band-Aider will fall back to full-file parsing."
  echo 'Install the optional accelerator with: pip install -e ".[model2vec]"'
else
  MODEL2VEC_INFO="$(
    "$PYTHON_BIN" -c '
from band_aider.judgement.model2vec_code_parser import (
    MODEL2VEC_MODEL_NAME,
    default_model2vec_cache_dir,
)

print(MODEL2VEC_MODEL_NAME)
print(default_model2vec_cache_dir())
'
  )"

  MODEL2VEC_NAME="$(printf '%s\n' "$MODEL2VEC_INFO" | sed -n '1p')"
  MODEL2VEC_CACHE_DIR="$(printf '%s\n' "$MODEL2VEC_INFO" | sed -n '2p')"

    # A missing cache is nonfatal: interactive users may download it, while
    # noninteractive benchmark runs continue without prompting.
    if ! "$PYTHON_BIN" -c '
from band_aider.judgement.model2vec_code_parser import model2vec_model_cached
import sys

sys.exit(0 if model2vec_model_cached() else 1)
' >/dev/null 2>&1; then
    if [ ! -t 0 ]; then
      echo "Model2Vec model cache is missing for $MODEL2VEC_NAME."
      echo "Band-Aider will fall back to full-file parsing."
    else
      echo "Band-Aider can use a local Model2Vec gate for narrow file parsing."
      echo "Missing model cache: $MODEL2VEC_NAME"
      echo "Cache root: $MODEL2VEC_CACHE_DIR"

      read -r -p "Download the Model2Vec cache now? [y/N]: " download_model2vec

      # The response branch either downloads the optional cache or records why
      # the fallback remains active; neither branch changes loop behavior.
      case "$download_model2vec" in
        [Yy] | [Yy][Ee][Ss])
          if ! "$PYTHON_BIN" -c '
from band_aider.judgement.model2vec_code_parser import (
    ensure_model2vec_model_downloaded,
)

ensure_model2vec_model_downloaded()
'; then
            echo "Failed to download $MODEL2VEC_NAME."
            echo "Band-Aider will fall back to full-file parsing."
          fi
          ;;
        *)
          echo "Model2Vec download declined."
          echo "Band-Aider will fall back to full-file parsing."
          ;;
      esac
    fi
  fi
fi

export BAND_AIDER_TOKEN_OVERFLOW_STRATEGY

EXTRA_ARGS=( )
# Forward the persistent repository container name/ID when provided. This is
# the call-site boundary that keeps container execution inside the CLI adapter.
if [ -n "${BAND_AIDER_REPO_CONTAINER:-}" ]; then
  EXTRA_ARGS+=("--repo-container" "$BAND_AIDER_REPO_CONTAINER")
fi

exec "$PYTHON_BIN" -m band_aider.cli "${EXTRA_ARGS[@]}" "$@"
