#!/bin/bash

# QAUP Docker 镜像版本验证脚本
# 验证配置文件中的版本与实际使用的版本是否一致

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_message() {
    local color=$1
    local message=$2
    echo -e "${color}${message}${NC}"
}

print_message $BLUE "========================================="
print_message $BLUE "QAUP Docker 镜像版本验证"
print_message $BLUE "========================================="
echo ""

# 加载配置文件
if [ -f "$SCRIPT_DIR/image-versions.env" ]; then
    source "$SCRIPT_DIR/image-versions.env"
    print_message $GREEN "✓ 已加载镜像版本配置文件"
else
    print_message $RED "✗ 镜像版本配置文件不存在"
    exit 1
fi

echo ""
print_message $BLUE "配置文件中的版本:"
echo "PostgreSQL/PostGIS: $POSTGRES_IMAGE"
echo "Redis: $REDIS_IMAGE"
echo "OpenJDK: $OPENJDK_IMAGE"
echo "Maven: $MAVEN_IMAGE"
echo "Nginx: $NGINX_IMAGE"
echo "Node.js: $NODE_IMAGE"
echo ""

# 检查 docker-compose.yml 中的版本
print_message $BLUE "docker-compose.yml 中的版本:"
if [ -f "$PROJECT_ROOT/docker-compose.yml" ]; then
    compose_postgres=$(grep -A 1 "qaup-postgres:" "$PROJECT_ROOT/docker-compose.yml" | grep "image:" | awk '{print $2}')
    compose_redis=$(grep -A 1 "qaup-redis:" "$PROJECT_ROOT/docker-compose.yml" | grep "image:" | awk '{print $2}')
    
    echo "PostgreSQL/PostGIS: $compose_postgres"
    echo "Redis: $compose_redis"
    
    # 验证一致性
    if [ "$compose_postgres" = "$POSTGRES_IMAGE" ]; then
        print_message $GREEN "✓ PostgreSQL 版本一致"
    else
        print_message $RED "✗ PostgreSQL 版本不一致"
        echo "  配置文件: $POSTGRES_IMAGE"
        echo "  docker-compose.yml: $compose_postgres"
    fi
    
    if [ "$compose_redis" = "$REDIS_IMAGE" ]; then
        print_message $GREEN "✓ Redis 版本一致"
    else
        print_message $RED "✗ Redis 版本不一致"
        echo "  配置文件: $REDIS_IMAGE"
        echo "  docker-compose.yml: $compose_redis"
    fi
else
    print_message $YELLOW "警告: docker-compose.yml 文件不存在"
fi

echo ""

# 检查 Dockerfile 中的版本
print_message $BLUE "Dockerfile 中的版本:"
if [ -f "$PROJECT_ROOT/Dockerfile" ]; then
    dockerfile_maven=$(grep "^FROM maven:" "$PROJECT_ROOT/Dockerfile" | awk '{print $2}' | head -1)
    dockerfile_openjdk=$(grep "^FROM openjdk:" "$PROJECT_ROOT/Dockerfile" | awk '{print $2}' | head -1)
    
    echo "Maven: $dockerfile_maven"
    echo "OpenJDK: $dockerfile_openjdk"
    
    # 验证一致性
    if [ "$dockerfile_maven" = "$MAVEN_IMAGE" ]; then
        print_message $GREEN "✓ Maven 版本一致"
    else
        print_message $RED "✗ Maven 版本不一致"
        echo "  配置文件: $MAVEN_IMAGE"
        echo "  Dockerfile: $dockerfile_maven"
    fi
    
    if [ "$dockerfile_openjdk" = "$OPENJDK_IMAGE" ]; then
        print_message $GREEN "✓ OpenJDK 版本一致"
    else
        print_message $RED "✗ OpenJDK 版本不一致"
        echo "  配置文件: $OPENJDK_IMAGE"
        echo "  Dockerfile: $dockerfile_openjdk"
    fi
else
    print_message $YELLOW "警告: Dockerfile 文件不存在"
fi

echo ""
print_message $BLUE "当前使用的版本总结:"
echo "✓ PostgreSQL 17 + PostGIS 3.5 (最新稳定版)"
echo "✓ Redis 8.0 (最新版本)"
echo "✓ OpenJDK 17 (LTS 版本)"
echo "✓ Maven 3.9.11 (最新稳定版)"
echo ""

print_message $YELLOW "升级注意事项:"
echo "1. PostgreSQL 17 包含性能改进和新特性"
echo "2. Redis 8.0 引入了新的数据结构和命令"
echo "3. 升级前请备份数据并在测试环境验证"
echo "4. 检查应用程序与新版本的兼容性"