#!/usr/bin/env bash

# Auto-activate uv virtual environment
if ! has uv; then
    echo "❌ uv is not installed. Please install it first:"
    echo "   python -m pip install pipx && pipx install uv"
    exit 1
fi

# Create virtual environment if it doesn't exist
if [[ ! -d ".venv" ]]; then
    echo "📦 Creating virtual environment with uv..."
    uv venv
fi

# Activate the virtual environment
source .venv/bin/activate
# Source .envrc.local if present (for user/machine-specific settings)
if [[ -f ".envrc.local" ]]; then
    echo "🔒 Sourcing .envrc.local..."
    source .envrc.local
fi

# Sync dependencies (including dev dependencies)
echo "🔄 Syncing dependencies..."
uv sync --all-extras

# Install pre-commit hooks if they don't exist
if [[ -f ".pre-commit-config.yaml" ]] && ! uv run pre-commit --version >/dev/null 2>&1; then
    echo "🪝 Installing pre-commit hooks..."
    uv run pre-commit install
fi

# Export environment variables
export PYTHONPATH="${PWD}/src:${PYTHONPATH}"
export UV_PROJECT_ENVIRONMENT="${PWD}/.venv"

echo "✅ Environment activated!"
echo "📁 Virtual environment: ${VIRTUAL_ENV}"
echo "🐍 Python: $(python --version)"
echo "📦 uv: $(uv --version)"

# Show available commands
echo ""
echo "🚀 Available commands:"
echo "   uv run task lint             # Lint (with auto-fix)"
echo "   uv run task format           # Format code"
echo "   uv run task type             # Type check"
echo "   uv run task test             # Run tests"
echo "   uv run task cov              # Run tests with coverage"
echo "   uv run task check            # lint + type + test"
