#!/bin/bash

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}🚀 Starting Veza local development environment...${NC}"

# Check prerequisites
echo -e "${YELLOW}📋 Checking prerequisites...${NC}"

command -v docker >/dev/null 2>&1 || { 
    echo -e "${RED}❌ Docker is required but not installed. Aborting.${NC}" >&2; 
    exit 1; 
}

command -v docker-compose >/dev/null 2>&1 || { 
    echo -e "${RED}❌ Docker Compose is required but not installed. Aborting.${NC}" >&2; 
    exit 1; 
}

# Check if Docker is running
if ! docker info >/dev/null 2>&1; then
    echo -e "${RED}❌ Docker daemon is not running. Please start Docker and try again.${NC}" >&2;
    exit 1;
fi

echo -e "${GREEN}✅ Prerequisites check passed${NC}"

# Copy .env.example if .env doesn't exist
if [ ! -f .env ]; then
    if [ -f .env.example ]; then
        echo -e "${YELLOW}📝 Creating .env file from .env.example...${NC}"
        cp .env.example .env
        echo -e "${YELLOW}⚠️  Please review and update .env file with your configuration${NC}"
    else
        echo -e "${YELLOW}⚠️  .env.example not found. You may need to create .env manually.${NC}"
    fi
fi

# Check if docker-compose.yml exists
if [ ! -f docker-compose.yml ]; then
    echo -e "${RED}❌ docker-compose.yml not found. Aborting.${NC}" >&2;
    exit 1;
fi

# Build and start services
echo -e "${BLUE}🔨 Building and starting services...${NC}"
docker-compose up -d --build

echo -e "${GREEN}✅ Services started successfully!${NC}"

# Wait for services to be ready
echo -e "${YELLOW}📊 Waiting for services to be healthy...${NC}"
sleep 10

# Check health status
echo -e "${BLUE}📋 Service status:${NC}"
docker-compose ps

# Check health endpoints
echo -e "${YELLOW}🔍 Checking health endpoints...${NC}"

# Function to check health endpoint
check_health() {
    local service=$1
    local url=$2
    local name=$3
    
    if curl -sf "$url" >/dev/null 2>&1; then
        echo -e "${GREEN}✅ ${name} is healthy${NC}"
        return 0
    else
        echo -e "${YELLOW}⚠️  ${name} health check failed (may still be starting)${NC}"
        return 1
    fi
}

# Check backend API
check_health "backend-api" "http://localhost:8080/health" "Backend API" || true

# Check chat server
check_health "chat-server" "http://localhost:8081/health" "Chat Server" || true

# Check stream server
check_health "stream-server" "http://localhost:8082/health" "Stream Server" || true

# Check frontend
check_health "frontend" "http://localhost:3000" "Frontend" || true

echo -e "${GREEN}🎉 Local development environment is ready!${NC}"
echo -e "${BLUE}📝 Access the services:${NC}"
echo -e "   - Frontend:      http://localhost:3000"
echo -e "   - Backend API:   http://localhost:8080"
echo -e "   - Chat Server:   ws://localhost:8081/ws"
echo -e "   - Stream Server: ws://localhost:8082/stream"
echo -e ""
echo -e "${YELLOW}💡 To stop services, run: docker-compose down${NC}"
echo -e "${YELLOW}💡 To view logs, run: docker-compose logs -f${NC}"

