#!/bin/bash

# Docker镜像测试构建脚本
# 用于快速验证Dockerfile语法和基础构建

set -e

# 配置变量
IMAGE_NAME="knowledge-base-app"
IMAGE_TAG="test"
PLATFORM="linux/amd64"

echo "开始测试Docker镜像构建..."
echo "镜像名称: ${IMAGE_NAME}:${IMAGE_TAG}"
echo "目标平台: ${PLATFORM}"

# 检查Docker是否运行
if ! docker info > /dev/null 2>&1; then
    echo "错误: Docker未运行或无法访问"
    exit 1
fi

# 只构建到base阶段进行快速测试
echo "正在构建基础镜像阶段..."
docker build \
    --platform ${PLATFORM} \
    --target base \
    --tag ${IMAGE_NAME}:${IMAGE_TAG}-base \
    --file Dockerfile \
    .

# 验证基础阶段构建结果
if [ $? -eq 0 ]; then
    echo "✅ 基础镜像构建成功!"
    
    # 显示镜像信息
    echo ""
    echo "基础镜像信息:"
    docker images ${IMAGE_NAME}:${IMAGE_TAG}-base
    
    # 检查镜像架构
    echo ""
    echo "镜像架构信息:"
    docker inspect ${IMAGE_NAME}:${IMAGE_TAG}-base --format='{{.Architecture}}'
    
    # 测试PHP版本
    echo ""
    echo "PHP版本信息:"
    docker run --rm ${IMAGE_NAME}:${IMAGE_TAG}-base php -v
    
    # 测试Pandoc
    echo ""
    echo "Pandoc版本信息:"
    docker run --rm ${IMAGE_NAME}:${IMAGE_TAG}-base pandoc --version | head -1
    
    echo ""
    echo "基础镜像测试完成!"
else
    echo "❌ 基础镜像构建失败!"
    exit 1
fi