#!/bin/bash

# Laravel知识库系统 - Docker配置测试脚本

set -e

echo "🧪 测试Docker配置..."

# 测试docker-compose配置语法
echo "📋 检查docker-compose.yml语法..."
if docker-compose config --quiet; then
    echo "✅ docker-compose.yml语法正确"
else
    echo "❌ docker-compose.yml语法错误"
    exit 1
fi

# 测试Dockerfile语法
echo "🐳 检查Dockerfile语法..."
if [ -f "Dockerfile" ]; then
    echo "✅ Dockerfile文件存在"
else
    echo "❌ Dockerfile文件不存在"
    exit 1
fi

# 检查必要的配置文件
echo "📁 检查配置文件..."

required_files=(
    "docker/mysql/my.cnf"
    "docker/redis/redis.conf"
    "docker/php/php.ini"
    "docker/supervisor/supervisord.conf"
)

for file in "${required_files[@]}"; do
    if [ -f "$file" ]; then
        echo "✅ $file 存在"
    else
        echo "❌ $file 不存在"
        exit 1
    fi
done

# 检查存储目录
echo "📂 检查存储目录..."
required_dirs=(
    "storage/mysql"
    "storage/redis"
    "storage/meilisearch"
    "storage/logs/app"
    "storage/logs/queue"
)

for dir in "${required_dirs[@]}"; do
    if [ -d "$dir" ]; then
        echo "✅ $dir 目录存在"
    else
        echo "⚠️ $dir 目录不存在，将创建..."
        mkdir -p "$dir"
        echo "✅ $dir 目录已创建"
    fi
done

# 检查脚本权限
echo "🔐 检查脚本权限..."
scripts=(
    "docker/start-production.sh"
    "docker/stop-production.sh"
    "docker/check-services.sh"
)

for script in "${scripts[@]}"; do
    if [ -x "$script" ]; then
        echo "✅ $script 可执行"
    else
        echo "⚠️ $script 不可执行，正在修复..."
        chmod +x "$script"
        echo "✅ $script 权限已修复"
    fi
done

# 检查环境变量模板
echo "🔧 检查环境配置..."
if [ -f ".env.production" ]; then
    echo "✅ .env.production 模板存在"
else
    echo "❌ .env.production 模板不存在"
    exit 1
fi

if [ -f ".env" ]; then
    echo "✅ .env 文件存在"
else
    echo "⚠️ .env 文件不存在，建议复制 .env.production"
fi

echo ""
echo "🎉 Docker配置测试完成！"
echo ""
echo "📝 下一步操作："
echo "1. 复制环境配置: cp .env.production .env"
echo "2. 编辑环境配置: nano .env"
echo "3. 启动服务: ./docker/start-production.sh"
echo "4. 检查状态: ./docker/check-services.sh"