#!/bin/bash

# Tank War Server部署验证脚本
set -e

echo "=== Tank War Server部署验证 ==="
echo ""

# 检查Kubernetes集群连接
echo "1. 检查Kubernetes集群连接..."
if kubectl cluster-info > /dev/null 2>&1; then
    echo "✓ Kubernetes集群连接正常"
    
    # 检查命名空间
    echo ""
    echo "2. 检查命名空间..."
    if kubectl get namespace tankwar > /dev/null 2>&1; then
        echo "✓ tankwar命名空间存在"
    else
        echo "✗ tankwar命名空间不存在"
        echo "   运行: kubectl create namespace tankwar"
        exit 1
    fi
    
    # 检查部署状态
    echo ""
    echo "3. 检查部署状态..."
    kubectl get deployment tankwar-server -n tankwar 2>/dev/null || {
        echo "✗ tankwar-server部署不存在"
        echo "   运行: kubectl apply -f k8s-deployment.yaml -n tankwar"
        exit 1
    }
    
    # 检查Pod状态
    echo ""
    echo "4. 检查Pod状态..."
    kubectl get pods -l app=tankwar-server -n tankwar
    
    # 检查服务状态
    echo ""
    echo "5. 检查服务状态..."
    kubectl get svc tankwar-server-service -n tankwar
    
    # 检查服务端点
    echo ""
    echo "6. 检查服务端点..."
    kubectl get endpoints tankwar-server-service -n tankwar
    
    # 检查Pod日志
    echo ""
    echo "7. 检查Pod日志（最近10行）..."
    kubectl logs -l app=tankwar-server -n tankwar --tail=10
    
    echo ""
    echo "=== 部署验证完成 ==="
    echo ""
    echo "如果所有检查都通过，服务应该正在运行。"
    echo "WebSocket连接地址格式：ws://<external-ip>:3000"
    echo ""
    echo "获取外部IP："
    echo "kubectl get svc tankwar-server-service -n tankwar -o jsonpath='{.status.loadBalancer.ingress[0].ip}'"
    
else
    echo "✗ Kubernetes集群连接失败"
    echo "请配置kubectl连接到正确的集群"
    echo ""
    echo "配置方法："
    echo "1. 获取集群kubeconfig文件"
    echo "2. 设置KUBECONFIG环境变量"
    echo "3. 或复制到 ~/.kube/config"
    exit 1
fi