#!/bin/bash

# Docker Compose 配置验证脚本
# 验证 Swoole 集成的 docker-compose.yml 配置

set -e

echo "开始验证 Docker Compose 配置..."

# 检查 docker-compose.yml 语法
echo "1. 检查 docker-compose.yml 语法..."
if docker-compose config > /dev/null 2>&1; then
    echo "✓ docker-compose.yml 语法正确"
else
    echo "✗ docker-compose.yml 语法错误"
    exit 1
fi

# 检查必要的服务是否存在
echo "2. 检查必要的服务..."
services=$(docker-compose config --services)

required_services=("app" "mysql" "redis" "meilisearch")
for service in "${required_services[@]}"; do
    if echo "$services" | grep -q "^$service$"; then
        echo "✓ 服务 $service 已配置"
    else
        echo "✗ 缺少服务 $service"
        exit 1
    fi
done

# 检查是否没有 Nginx 服务（应该被移除）
if echo "$services" | grep -q "^nginx$"; then
    echo "✗ 发现 Nginx 服务，应该已被移除"
    exit 1
else
    echo "✓ Nginx 服务已正确移除"
fi

# 检查应用服务的端口配置
echo "3. 检查端口配置..."
app_ports=$(docker-compose config | grep -A 20 "app:" | grep -A 5 "ports:" | grep "8000")
if [ -n "$app_ports" ]; then
    echo "✓ 应用服务端口 8000 已正确配置"
else
    echo "✗ 应用服务端口配置错误"
    exit 1
fi

# 检查健康检查配置
echo "4. 检查健康检查配置..."
healthcheck=$(docker-compose config | grep -A 10 "healthcheck:" | grep "swoole-health-check")
if [ -n "$healthcheck" ]; then
    echo "✓ Swoole 健康检查已配置"
else
    echo "✗ Swoole 健康检查配置缺失"
    exit 1
fi

# 检查服务依赖关系
echo "5. 检查服务依赖关系..."
depends_on=$(docker-compose config | grep -A 10 "depends_on:")
if echo "$depends_on" | grep -q "mysql" && echo "$depends_on" | grep -q "redis" && echo "$depends_on" | grep -q "meilisearch"; then
    echo "✓ 服务依赖关系正确配置"
else
    echo "✗ 服务依赖关系配置错误"
    exit 1
fi

echo "✓ 所有配置验证通过！"
echo "Docker Compose 配置已成功更新为 Swoole 架构"