#!/bin/bash
# 服务验证脚本 - 验证部署后服务状态

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

print_success() {
    echo -e "${GREEN}[SUCCESS] $1${NC}"
}

print_error() {
    echo -e "${RED}[ERROR] $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}[WARNING] $1${NC}"
}

print_info() {
    echo -e "${BLUE}[INFO] $1${NC}"
}

# 1. 容器状态检查
echo "=========================================="
echo "1. 容器状态检查"
echo "=========================================="

echo ""
echo "正在检查所有容器..."
non_running=$(podman ps -a | grep -v 'Up' | grep -v 'NAMES' | wc -l)
if [ "$non_running" -eq 0 ]; then
    print_success "所有容器运行正常"
else
    print_error "有 $non_running 个容器未正常启动"
    podman ps -a
    exit 1
fi

# 2. 端口连通性检查
echo ""
echo "=========================================="
echo "2. 端口连通性检查"
echo "=========================================="

ports=("80" "443" "8080" "8848" "3000" "9090")
for port in "${ports[@]}"; do
    echo "检查端口 $port..."
    if nc -z localhost "$port" 2>/dev/null; then
        print_success "端口 $port 正常监听"
    else
        print_error "端口 $port 未监听"
        exit 1
    fi
done

# 3. API健康检查
echo ""
echo "=========================================="
echo "3. API健康检查"
echo "=========================================="

echo "检查API网关..."
gateway_response=$(curl -s http://localhost:8080/actuator/health)
if echo "$gateway_response" | grep -q "UP"; then
    print_success "API网关健康检查通过"
else
    print_error "API网关健康检查失败: $gateway_response"
    exit 1
fi

echo "检查Nginx..."
nginx_response=$(curl -s http://localhost/health)
if echo "$nginx_response" | grep -q "UP"; then
    print_success "Nginx健康检查通过"
else
    print_error "Nginx健康检查失败: $nginx_response"
    exit 1
fi

echo "检查Nacos..."
nacos_response=$(curl -s http://localhost:8848/nacos/v1/ns/operator/metrics)
if [ -n "$nacos_response" ]; then
    print_success "Nacos健康检查通过"
else
    print_error "Nacos健康检查失败"
    exit 1
fi

# 4. 前端页面检查
echo ""
echo "=========================================="
echo "4. 前端页面检查"
echo "=========================================="

echo "检查平台端页面..."
platform_response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost/platform/index.html)
if [ "$platform_response" -eq 200 ]; then
    print_success "平台端页面正常"
else
    print_error "平台端页面异常: HTTP $platform_response"
    exit 1
fi

echo "检查商家端页面..."
merchant_response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost/merchant/index.html)
if [ "$merchant_response" -eq 200 ]; then
    print_success "商家端页面正常"
else
    print_error "商家端页面异常: HTTP $merchant_response"
    exit 1
fi

echo "检查官网页面..."
official_response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost/official/index.html)
if [ "$official_response" -eq 200 ]; then
    print_success "官网页面正常"
else
    print_error "官网页面异常: HTTP $official_response"
    exit 1
fi

# 5. 服务注册检查
echo ""
echo "=========================================="
echo "5. Nacos服务注册检查"
echo "=========================================="

service_count=$(curl -s 'http://localhost:8848/nacos/v1/ns/service/list?pageNo=1&pageSize=100' | python3 -c "import sys,json; data=json.load(sys.stdin); print(len(data.get('doms', [])))")
echo "已注册服务数量: $service_count"
if [ "$service_count" -gt 0 ]; then
    print_success "服务注册正常"
else
    print_warning "未检测到注册服务"
fi

# 6. 输出验证报告
echo ""
echo "=========================================="
echo "🎉 验证完成!"
echo "=========================================="
echo ""
echo "验证报告:"
echo "- 容器状态: ✅ 所有容器运行正常"
echo "- 端口监听: ✅ 所有端口正常"
echo "- API健康: ✅ 所有API健康检查通过"
echo "- 前端页面: ✅ 所有页面正常"
echo "- 服务注册: ✅ Nacos服务注册正常"
echo ""
echo "部署验证通过!"

# 保存验证报告
echo "验证时间: $(date)" > /opt/FzxBDSH/deploy_verify.log
echo "验证结果: 通过" >> /opt/FzxBDSH/deploy_verify.log
echo "服务数量: $service_count" >> /opt/FzxBDSH/deploy_verify.log
