#!/bin/bash
# Restart script for Church Music App services

echo "🔄 Restarting Church Music App Services..."

# Stop services
echo "⏸️  Stopping services..."
pkill -f "gunicorn.*church_music_backend" || true
pkill -f "serve.*5100" || true
sleep 2

# Start backend
echo "🚀 Starting backend..."
cd /media/pts/Website/Church_HOP_MusicData/backend
source venv/bin/activate
nohup gunicorn \
    --bind 127.0.0.1:8080 \
    --workers 2 \
    --worker-class sync \
    --timeout 120 \
    --graceful-timeout 30 \
    --keep-alive 5 \
    --access-logfile logs/access.log \
    --error-logfile logs/error.log \
    --log-level info \
    --name church_music_backend \
    app:app > logs/backend.log 2>&1 &

echo "⏳ Waiting for backend to start..."
sleep 3

# Start frontend
echo "🚀 Starting frontend..."
cd /media/pts/Website/Church_HOP_MusicData/frontend
nohup serve -s -p 5100 --no-clipboard build > ../frontend.log 2>&1 &

echo "⏳ Waiting for frontend to start..."
sleep 2

# Check status
echo ""
echo "📊 Service Status:"
echo "Backend: $(curl -s http://localhost:8080/api/health | grep -o 'ok' || echo '❌ Not responding')"
echo "Frontend: $(curl -s http://localhost:5100 | grep -o 'HOP Worship' | head -1 || echo '❌ Not responding')"
echo ""
echo "✅ Services restarted!"
echo "🌐 Frontend: http://localhost:5100"
echo "🔧 Backend API: http://localhost:8080"
