#!/bin/bash
# =========================================================
# BreakPilot — Start All Projects
# =========================================================
# Usage: ./start-all.sh [--core-only] [--no-compliance]
# =========================================================

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
LEHRER_ROOT="$(dirname "$PROJECT_ROOT")/breakpilot-lehrer"
COMPLIANCE_ROOT="$(dirname "$PROJECT_ROOT")/breakpilot-compliance"

DOCKER="/usr/local/bin/docker"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

echo -e "${GREEN}=========================================${NC}"
echo -e "${GREEN} BreakPilot — Starting All Projects${NC}"
echo -e "${GREEN}=========================================${NC}"

# Phase 1: Core
echo -e "\n${YELLOW}[1/3] Starting Core Infrastructure...${NC}"
$DOCKER compose -f "$PROJECT_ROOT/docker-compose.yml" up -d
echo -e "${GREEN}Core started. Waiting for health check...${NC}"

# Wait for health aggregator
MAX_WAIT=120
WAITED=0
until curl -sf http://127.0.0.1:8099/health > /dev/null 2>&1; do
    sleep 5
    WAITED=$((WAITED + 5))
    if [ $WAITED -ge $MAX_WAIT ]; then
        echo -e "${RED}Core health check timeout after ${MAX_WAIT}s${NC}"
        echo "Check: $DOCKER compose -f $PROJECT_ROOT/docker-compose.yml logs"
        exit 1
    fi
    echo "  Waiting for core... (${WAITED}s/${MAX_WAIT}s)"
done
echo -e "${GREEN}Core is healthy!${NC}"

if [ "$1" = "--core-only" ]; then
    echo -e "\n${GREEN}Done (core-only mode).${NC}"
    exit 0
fi

# Phase 2: Lehrer
if [ -f "$LEHRER_ROOT/docker-compose.yml" ]; then
    echo -e "\n${YELLOW}[2/3] Starting Lehrer Platform...${NC}"
    $DOCKER compose -f "$LEHRER_ROOT/docker-compose.yml" up -d
    echo -e "${GREEN}Lehrer platform started.${NC}"
else
    echo -e "\n${YELLOW}[2/3] Skipping Lehrer (not found: $LEHRER_ROOT)${NC}"
fi

# Phase 3: Compliance
if [ "$1" = "--no-compliance" ]; then
    echo -e "\n${YELLOW}[3/3] Skipping Compliance (--no-compliance flag)${NC}"
elif [ -f "$COMPLIANCE_ROOT/docker-compose.yml" ]; then
    echo -e "\n${YELLOW}[3/3] Starting Compliance Platform...${NC}"
    $DOCKER compose -f "$COMPLIANCE_ROOT/docker-compose.yml" up -d
    echo -e "${GREEN}Compliance platform started.${NC}"
else
    echo -e "\n${YELLOW}[3/3] Skipping Compliance (not found: $COMPLIANCE_ROOT)${NC}"
fi

echo -e "\n${GREEN}=========================================${NC}"
echo -e "${GREEN} All Projects Started!${NC}"
echo -e "${GREEN}=========================================${NC}"
echo ""
echo "URLs:"
echo "  Core Health:       http://macmini:8099/health"
echo "  Studio v2:         https://macmini/"
echo "  Admin Lehrer:      https://macmini:3002/"
echo "  Admin Compliance:  https://macmini:3007/"
echo "  Backend Core:      https://macmini:8000/"
echo "  Backend Lehrer:    https://macmini:8001/"
echo "  Backend Compliance:https://macmini:8002/"
echo "  RAG Service:       https://macmini:8097/"
