#!/bin/bash
#
# Complete Church Music Database Startup Script
# Ensures clean startup with no conflicts
#

set -e

echo "=========================================="
echo "Church Music Database - Startup Script"
echo "=========================================="
echo ""

# Step 1: Kill all development servers
echo "Step 1: Cleaning up development servers..."
/media/pts/Website/Church_HOP_MusicData/kill-dev-servers.sh
echo ""

# Step 2: Stop any running production services
echo "Step 2: Stopping existing production services..."
sudo systemctl stop church-music-frontend.service church-music-backend.service 2>/dev/null || true
sleep 2
echo "✓ Services stopped"
echo ""

# Step 3: Verify ports are free
echo "Step 3: Verifying ports..."
if lsof -i :8080 > /dev/null 2>&1; then
    echo "⚠ Port 8080 still in use, force killing..."
    sudo lsof -i :8080 -t | xargs -r sudo kill -9 || true
    sleep 1
fi
if lsof -i :5100 > /dev/null 2>&1; then
    echo "⚠ Port 5100 still in use, force killing..."
    sudo lsof -i :5100 -t | xargs -r sudo kill -9 || true
    sleep 1
fi
echo "✓ Ports 8080 and 5100 are free"
echo ""

# Step 4: Reset failed services
echo "Step 4: Resetting service states..."
sudo systemctl reset-failed 2>/dev/null || true
echo "✓ Service states reset"
echo ""

# Step 5: Start production services
echo "Step 5: Starting production services..."
sudo systemctl start church-music-backend.service
sleep 3
sudo systemctl start church-music-frontend.service
sleep 2
echo "✓ Services started"
echo ""

# Step 6: Verify services are running
echo "Step 6: Verifying services..."
echo ""

if sudo systemctl is-active --quiet church-music-backend.service; then
    echo "✅ Backend service: RUNNING"
else
    echo "❌ Backend service: FAILED"
    sudo systemctl status church-music-backend.service --no-pager -l
    exit 1
fi

if sudo systemctl is-active --quiet church-music-frontend.service; then
    echo "✅ Frontend service: RUNNING"
else
    echo "❌ Frontend service: FAILED"
    sudo systemctl status church-music-frontend.service --no-pager -l
    exit 1
fi

# Step 7: Test endpoints
echo ""
echo "Step 7: Testing endpoints..."
echo ""

BACKEND_TEST=$(curl -s http://localhost:8080/api/health | grep -o "ok" || echo "")
if [ "$BACKEND_TEST" = "ok" ]; then
    echo "✅ Backend API: RESPONDING (http://localhost:8080/api/health)"
else
    echo "❌ Backend API: NOT RESPONDING"
    exit 1
fi

FRONTEND_TEST=$(curl -s http://localhost:5100/ | grep -o "House of Prayer" || echo "")
if [ -n "$FRONTEND_TEST" ]; then
    echo "✅ Frontend: RESPONDING (http://localhost:5100/)"
else
    echo "❌ Frontend: NOT RESPONDING"
    exit 1
fi

# Step 8: Check auto-start configuration
echo ""
echo "Step 8: Verifying auto-start configuration..."
echo ""

if systemctl is-enabled --quiet church-music-backend.service; then
    echo "✅ Backend auto-start: ENABLED"
else
    echo "⚠ Backend auto-start: DISABLED"
fi

if systemctl is-enabled --quiet church-music-frontend.service; then
    echo "✅ Frontend auto-start: ENABLED"
else
    echo "⚠ Frontend auto-start: DISABLED"
fi

# Step 9: Display status
echo ""
echo "=========================================="
echo "✅ ALL SYSTEMS OPERATIONAL"
echo "=========================================="
echo ""
echo "Services Status:"
sudo systemctl status church-music-backend.service church-music-frontend.service --no-pager | grep -E "(Active:|Main PID:|Tasks:|Memory:)"
echo ""
echo "Access URLs:"
echo "  Backend API:  http://localhost:8080/api/health"
echo "  Frontend:     http://localhost:5100/"
echo "  Public HTTPS: https://houseofprayer.ddns.net/"
echo ""
echo "Logs:"
echo "  Backend:  journalctl -u church-music-backend.service -f"
echo "  Frontend: journalctl -u church-music-frontend.service -f"
echo ""
echo "=========================================="

exit 0
