#!/bin/bash

# Tank War Server部署测试脚本
set -e

echo "=== Tank War Server部署测试 ==="
echo ""

# 检查必需文件是否存在
echo "1. 检查必需文件..."
files=("Dockerfile" "k8s-deployment.yaml" "index.js" "package.json" "deploy.sh")
for file in "${files[@]}"; do
    if [ -f "$file" ]; then
        echo "✓ $file 存在"
    else
        echo "✗ $file 缺失"
        exit 1
    fi
done

echo ""
echo "2. 检查Dockerfile语法..."
if grep -q "FROM node" Dockerfile && grep -q "EXPOSE 3000" Dockerfile; then
    echo "✓ Dockerfile语法正确"
else
    echo "✗ Dockerfile语法错误"
    exit 1
fi

echo ""
echo "3. 检查K8s配置文件..."
# 检查基本的YAML语法
if python3 -c "import yaml; yaml.safe_load(open('k8s-deployment.yaml'))" > /dev/null 2>&1; then
    echo "✓ K8s配置文件语法正确"
else
    echo "⚠ 无法验证K8s配置，需要连接到Kubernetes集群"
    echo "   运行 'kubectl cluster-info' 检查连接状态"
fi

echo ""
echo "4. 检查Node.js依赖..."
if node -e "require('./package.json')" > /dev/null 2>&1; then
    echo "✓ package.json语法正确"
else
    echo "✗ package.json语法错误"
    exit 1
fi

echo ""
echo "5. 检查服务器代码..."
if node -c index.js > /dev/null 2>&1; then
    echo "✓ index.js语法正确"
else
    echo "✗ index.js语法错误"
    exit 1
fi

echo ""
echo "6. 检查部署脚本权限..."
chmod +x deploy.sh
if [ -x "deploy.sh" ]; then
    echo "✓ 部署脚本可执行"
else
    echo "✗ 部署脚本权限错误"
    exit 1
fi

echo ""
echo "=== 所有检查通过！ ==="
echo ""
echo "下一步："
echo "1. 确保Docker守护进程正在运行"
echo "2. 确保kubectl已配置正确的集群上下文"
echo "3. 运行 ./deploy.sh 开始部署"
echo ""