# direnv config for combaero (root)
#
# This single .envrc manages a shared virtual environment at the repository root
# for all Python-related tasks: building the combaero wheel, running thermo_data_generator
# scripts, and executing Python tests.

# Re-run this .envrc when key files change
watch_file .envrc
watch_file pyproject.toml
watch_dir python
watch_dir thermo_data_generator

VENV_DIR="$PWD/.venv"
PYTHON_BIN="$VENV_DIR/bin/python"

# 1) Create venv if missing (prefer uv, fall back to python -m venv)
if [ ! -d "$VENV_DIR" ]; then
  if command -v uv >/dev/null 2>&1; then
    echo "[combaero] Creating virtualenv with uv at $VENV_DIR"
    uv venv "$VENV_DIR"
  else
    echo "[combaero] uv not found, creating virtualenv with python -m venv at $VENV_DIR"
    python3 -m venv "$VENV_DIR"
  fi
fi

# 2) Activate venv
export VIRTUAL_ENV="$VENV_DIR"
export PATH="$VENV_DIR/bin:$PATH"

# 3) Build-related env flags
export COMBAERO_BUILD_PYTHON=1
export PIP_DISABLE_PIP_VERSION_CHECK=1

# 4) Auto-install dependencies via uv (once per venv)
if command -v uv >/dev/null 2>&1; then
  MARKER="$VENV_DIR/.uv-bootstrap-done"
  if [ ! -f "$MARKER" ]; then
    echo "[combaero] Bootstrapping Python dependencies via uv"

    # Install core tooling: pip, build system, and pybind11 for the wheel
    uv pip install --python "$PYTHON_BIN" pip build scikit-build-core pybind11

    # Install dev dependencies (pytest)
    uv pip install --python "$PYTHON_BIN" pytest

    # Install thermo_data_generator dependencies
    uv pip install --python "$PYTHON_BIN" pandas pyyaml

    touch "$MARKER"
    echo "[combaero] Bootstrap complete. Dependencies installed."
  fi
fi
