#!/bin/bash

# 环境变量验证脚本
# 用于验证Docker部署所需的环境变量是否正确配置

set -e

echo "==================================="
echo "环境变量验证脚本"
echo "==================================="

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 错误计数
ERROR_COUNT=0

# 验证函数
validate_var() {
    local var_name=$1
    local var_value=$2
    local is_required=${3:-true}
    local description=$4

    if [ -z "$var_value" ]; then
        if [ "$is_required" = true ]; then
            echo -e "${RED}✗ 错误: $var_name 未设置${NC} - $description"
            ((ERROR_COUNT++))
        else
            echo -e "${YELLOW}⚠ 警告: $var_name 未设置${NC} - $description (可选)"
        fi
    else
        echo -e "${GREEN}✓ $var_name 已设置${NC} - $description"
    fi
}

# 验证必需的环境变量
echo "检查必需的环境变量..."
echo "-----------------------------------"

validate_var "APP_KEY" "$APP_KEY" true "应用加密密钥"
validate_var "DB_PASSWORD" "$DB_PASSWORD" true "数据库密码"
validate_var "MEILISEARCH_KEY" "$MEILISEARCH_KEY" true "Meilisearch主密钥"

echo ""
echo "检查应用配置..."
echo "-----------------------------------"

validate_var "APP_NAME" "$APP_NAME" false "应用名称"
validate_var "APP_ENV" "$APP_ENV" false "应用环境"
validate_var "APP_DEBUG" "$APP_DEBUG" false "调试模式"
validate_var "APP_URL" "$APP_URL" false "应用URL"

echo ""
echo "检查数据库配置..."
echo "-----------------------------------"

validate_var "DB_CONNECTION" "$DB_CONNECTION" false "数据库连接类型"
validate_var "DB_HOST" "$DB_HOST" false "数据库主机"
validate_var "DB_PORT" "$DB_PORT" false "数据库端口"
validate_var "DB_DATABASE" "$DB_DATABASE" false "数据库名称"
validate_var "DB_USERNAME" "$DB_USERNAME" false "数据库用户名"

echo ""
echo "检查Redis配置..."
echo "-----------------------------------"

validate_var "REDIS_HOST" "$REDIS_HOST" false "Redis主机"
validate_var "REDIS_PORT" "$REDIS_PORT" false "Redis端口"
validate_var "REDIS_PASSWORD" "$REDIS_PASSWORD" false "Redis密码"

echo ""
echo "检查Meilisearch配置..."
echo "-----------------------------------"

validate_var "MEILISEARCH_HOST" "$MEILISEARCH_HOST" false "Meilisearch主机"
validate_var "SCOUT_DRIVER" "$SCOUT_DRIVER" false "搜索驱动"

echo ""
echo "检查邮件配置..."
echo "-----------------------------------"

validate_var "MAIL_MAILER" "$MAIL_MAILER" false "邮件驱动"
if [ "$MAIL_MAILER" = "smtp" ]; then
    validate_var "MAIL_HOST" "$MAIL_HOST" true "SMTP主机"
    validate_var "MAIL_PORT" "$MAIL_PORT" true "SMTP端口"
    validate_var "MAIL_USERNAME" "$MAIL_USERNAME" true "SMTP用户名"
    validate_var "MAIL_PASSWORD" "$MAIL_PASSWORD" true "SMTP密码"
fi

echo ""
echo "==================================="

# 检查APP_KEY格式
if [ -n "$APP_KEY" ]; then
    if [[ $APP_KEY == base64:* ]]; then
        echo -e "${GREEN}✓ APP_KEY 格式正确${NC}"
    else
        echo -e "${RED}✗ 错误: APP_KEY 格式不正确，应该以 'base64:' 开头${NC}"
        ((ERROR_COUNT++))
    fi
fi

# 检查默认密码
if [ "$DB_PASSWORD" = "secure_password_change_this" ] || [ "$DB_PASSWORD" = "secure_password_change_this_in_production" ]; then
    echo -e "${YELLOW}⚠ 警告: 数据库密码使用默认值，建议更改${NC}"
fi

if [ "$MEILISEARCH_KEY" = "your-master-key-change-this-in-production" ]; then
    echo -e "${YELLOW}⚠ 警告: Meilisearch密钥使用默认值，建议更改${NC}"
fi

# 检查环境特定配置
if [ "$APP_ENV" = "production" ]; then
    echo ""
    echo "生产环境额外检查..."
    echo "-----------------------------------"
    
    if [ "$APP_DEBUG" = "true" ]; then
        echo -e "${YELLOW}⚠ 警告: 生产环境不建议启用调试模式${NC}"
    fi
    
    if [ "$LOG_LEVEL" = "debug" ]; then
        echo -e "${YELLOW}⚠ 警告: 生产环境建议使用 info 或更高级别的日志${NC}"
    fi
fi

echo "==================================="

# 输出结果
if [ $ERROR_COUNT -eq 0 ]; then
    echo -e "${GREEN}✓ 环境变量验证通过！${NC}"
    echo "可以继续进行Docker部署。"
    exit 0
else
    echo -e "${RED}✗ 发现 $ERROR_COUNT 个错误${NC}"
    echo "请修复上述错误后再进行部署。"
    exit 1
fi