#!/bin/bash
# Script to build and publish route402 Python package to PyPI

set -e

echo "📦 Route402 Python SDK - Build & Publish Script"
echo "================================================"
echo ""

# Check if we're in the right directory
if [ ! -f "pyproject.toml" ]; then
    echo "❌ Error: pyproject.toml not found. Run this script from packages/sdk-python/"
    exit 1
fi

# Step 1: Clean previous builds
echo "🧹 Cleaning previous builds..."
rm -rf dist/ build/ *.egg-info/
echo "✅ Cleaned"
echo ""

# Step 2: Install build dependencies
echo "📥 Installing build dependencies..."
python3 -m pip install --upgrade pip build twine --quiet
echo "✅ Dependencies installed"
echo ""

# Step 3: Build package
echo "🔨 Building package..."
python3 -m build
echo "✅ Package built"
echo ""

# Step 4: Check build
echo "🔍 Checking package..."
python3 -m twine check dist/*
echo "✅ Package check passed"
echo ""

# Step 5: Show package info
echo "📊 Package info:"
ls -lh dist/
echo ""

# Step 6: Upload to PyPI (ask for confirmation)
echo "🚀 Ready to publish to PyPI!"
echo ""
read -p "Do you want to publish now? (y/N): " -n 1 -r
echo ""

if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "📤 Publishing to PyPI..."
    python3 -m twine upload dist/*
    echo ""
    echo "✅ Published successfully!"
    echo ""
    echo "📦 Install with: pip install route402"
    echo "📚 View at: https://pypi.org/project/route402/"
else
    echo "⏸️  Publishing cancelled"
    echo ""
    echo "To publish later, run:"
    echo "  python3 -m twine upload dist/*"
fi

echo ""
echo "🎉 Done!"
