#!/bin/bash

# Local Development Quick Start Script

echo "🚀 Starting H2H Local Development Environment..."
echo ""

# Check if Docker is running
if ! docker info > /dev/null 2>&1; then
    echo "❌ Docker is not running. Please start Docker Desktop and try again."
    exit 1
fi

echo "✅ Docker is running"
echo ""

# Start services
echo "📦 Building and starting services..."
docker compose -f docker-compose.dev.yml up --build -d

echo ""
echo "⏳ Waiting for services to be ready..."
sleep 5

# Check if services are running
BACKEND_STATUS=$(docker compose -f docker-compose.dev.yml ps backend --status running --format json 2>/dev/null | grep -c "running" || echo "0")
FRONTEND_STATUS=$(docker compose -f docker-compose.dev.yml ps frontend --status running --format json 2>/dev/null | grep -c "running" || echo "0")

if [ "$BACKEND_STATUS" = "1" ] && [ "$FRONTEND_STATUS" = "1" ]; then
    echo ""
    echo "✅ All services are running!"
    echo ""
    echo "🌐 Access your application:"
    echo "   Frontend:  http://localhost:5173"
    echo "   Backend:   http://localhost:8000"
    echo "   API Docs:  http://localhost:8000/docs"
    echo ""
    echo "👤 Test Users:"
    echo "   alice@example.com   / password123"
    echo "   bob@example.com     / password123"
    echo "   charlie@example.com / password123"
    echo ""
    echo "📊 View logs:"
    echo "   docker compose -f docker-compose.dev.yml logs -f"
    echo ""
    echo "🛑 Stop services:"
    echo "   docker compose -f docker-compose.dev.yml down"
    echo ""
else
    echo ""
    echo "⚠️  Services may not be ready yet. Check status with:"
    echo "   docker compose -f docker-compose.dev.yml ps"
    echo ""
    echo "📋 View logs:"
    echo "   docker compose -f docker-compose.dev.yml logs -f"
fi
