#!/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

echo "✅ Environment ready"
