# UV-based development environment setup
# Automatically creates venv and installs dependencies in editable mode

# Load local migration config if present (gitignored)
if [ -f .env ]; then
    source_env .env
fi

# Check if uv is installed
if ! command -v uv >/dev/null 2>&1; then
    echo "Error: uv is not installed. Install it from https://docs.astral.sh/uv/getting-started/installation/"
    exit 1
fi

# Set the virtual environment path
export VIRTUAL_ENV=".venv"

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

# Activate the virtual environment
source "$VIRTUAL_ENV/bin/activate"

# Sync dependencies and install project in editable mode
# This runs on every directory entry to ensure dependencies are up-to-date
watch_file pyproject.toml
watch_file uv.lock
if [ -f "pyproject.toml" ]; then
    echo "Syncing dependencies with uv..."
    uv sync --frozen
fi

# Add project root to PYTHONPATH (useful for imports)
PATH_add "$(pwd)"

echo "✅ UV environment activated (.venv)"
echo "   Python: $(python --version)"
echo "   Package: frappe-manager (editable mode)"
