#!/bin/bash
# Start mainroom services (amar + soleprint)
#
# Usage:
#   ./start.sh              # Start all (foreground, see logs)
#   ./start.sh <service>    # Start specific service (e.g., amar, soleprint)
#   ./start.sh -d           # Start all (detached)
#   ./start.sh --build      # Start with rebuild
#   ./start.sh -d --build   # Start detached with rebuild
#   ./start.sh --with-nginx # Start with nginx container (local dev only)

set -e

# Change to parent directory (services are in ../service_name)
cd "$(dirname "$0")/.."

# Export mainroom/.env vars so child docker-compose files can use them
if [ -f ".env" ]; then
    set -a
    source .env
    set +a
fi

TARGET="all"
DETACH=""
BUILD=""
WITH_NGINX=""
SERVICE_DIRS=()

# Find all service directories (have docker-compose.yml, exclude ctrl/nginx)
for dir in */; do
    dirname="${dir%/}"
    if [ -f "$dir/docker-compose.yml" ] && [ "$dirname" != "ctrl" ] && [ "$dirname" != "nginx" ]; then
        SERVICE_DIRS+=("$dirname")
    fi
done

for arg in "$@"; do
    case $arg in
        -d|--detached) DETACH="-d" ;;
        --build) BUILD="--build" ;;
        --with-nginx) WITH_NGINX="true" ;;
        all) TARGET="all" ;;
        *)
            # Check if it's a valid service directory
            if [[ " ${SERVICE_DIRS[@]} " =~ " ${arg} " ]]; then
                TARGET="$arg"
            fi
            ;;
    esac
done

start_service() {
    local service=$1
    echo "Starting $service..."
    cd "$service"

    # If --with-nginx and service is soleprint, include nginx compose
    if [ "$WITH_NGINX" = "true" ] && [ "$service" = "soleprint" ]; then
        echo "  Including nginx container..."
        DOCKER_BUILDKIT=0 COMPOSE_DOCKER_CLI_BUILD=0 docker compose -f docker-compose.yml -f docker-compose.nginx.yml up $DETACH $BUILD
    else
        DOCKER_BUILDKIT=0 COMPOSE_DOCKER_CLI_BUILD=0 docker compose up $DETACH $BUILD
    fi

    cd ..
    [ -n "$DETACH" ] && echo "  $service started"
}

if [ "$TARGET" = "all" ]; then
    if [ -z "$DETACH" ]; then
        # Foreground mode: start all services in parallel
        echo "Starting all services (foreground): ${SERVICE_DIRS[*]}"
        PIDS=()
        for service in "${SERVICE_DIRS[@]}"; do
            cd "$service"
            DOCKER_BUILDKIT=0 COMPOSE_DOCKER_CLI_BUILD=0 docker compose up $BUILD &
            PIDS+=($!)
            cd ..
        done
        # Wait for all processes
        wait "${PIDS[@]}"
    else
        # Detached mode: start sequentially
        for service in "${SERVICE_DIRS[@]}"; do
            start_service "$service"
            echo ""
        done
    fi
elif [[ " ${SERVICE_DIRS[@]} " =~ " ${TARGET} " ]]; then
    start_service "$TARGET"
else
    echo "Usage: ./start.sh [${SERVICE_DIRS[*]}|all] [-d|--detached] [--build]"
    exit 1
fi

if [ -n "$DETACH" ]; then
    echo ""
    echo "=== Services Started ==="
    echo ""
    docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep -E "(mainroom|amar|soleprint|NAMES)"
fi
