#!/usr/bin/env bash

# Auto-activate uv virtual environment
if ! has uv; then
    echo "❌ uv is not installed. Please install it first:"
    echo "   curl -LsSf https://astral.sh/uv/install.sh | sh"
    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..."
# Use --all-extras for maximum compatibility with any dependency structure
# (works with both dependency-groups and optional-dependencies)
uv sync --all-extras

# Git configuration for this project (optional)
# Uncomment and customize as needed or add to .envrc.local:
# git config user.name "Your Name"
# git config user.email "your.email@example.com"

# 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 pytest                 # Run tests"
echo "   uv run pytest --cov           # Run tests with coverage report"
echo "   uv run pytest --cov --cov-report=html  # Generate HTML coverage report"
echo "   uv run ruff check             # Lint code"
echo "   uv run ruff format            # Format code"
echo "   uv run mypy src/              # Type check"
echo "   uv run pre-commit run --all   # Run all pre-commit hooks"
# Show available taskipy commands if task is available
if has task; then
    echo ""
    echo "📝 Available taskipy commands (via 'task'):"
    task --list
fi
