#!/bin/bash

echo "🚀 Starting DonShare Backend API..."

# Vérifier si Python est installé
if ! command -v python3 &> /dev/null; then
    echo "❌ Python 3 is not installed. Please install Python 3.8+ first."
    exit 1
fi

# Vérifier si pip est installé
if ! command -v pip &> /dev/null; then
    echo "❌ pip is not installed. Please install pip first."
    exit 1
fi

# Créer l'environnement virtuel s'il n'existe pas
if [ ! -d "venv" ]; then
    echo "📦 Creating virtual environment..."
    python3 -m venv venv
fi

# Activer l'environnement virtuel
echo "🔧 Activating virtual environment..."
source venv/bin/activate

# Installer les dépendances
echo "📥 Installing dependencies..."
pip install -r requirements.txt

# Vérifier si le fichier .env existe
if [ ! -f ".env" ]; then
    echo "⚙️ Creating .env file from template..."
    cp env.example .env
    echo "⚠️  Please edit .env file with your configuration before starting the server."
    echo "   Press Enter to continue or Ctrl+C to exit..."
    read
fi

# Créer le dossier uploads s'il n'existe pas
mkdir -p uploads

echo "✅ Setup complete!"
echo "🌐 Starting server on http://localhost:8000"
echo "📚 API documentation: http://localhost:8000/docs"
echo "🛑 Press Ctrl+C to stop the server"
echo ""

# Démarrer le serveur
python run.py
