#!/bin/bash
set -e

# ==========================================
# 定数定義
# ==========================================
LAMBDA_BUILD_DIR="/dist/package"
LAMBDA_ZIP_PATH="/dist/deployment.zip"

if [ "$DOCKER_PLATFORM" = "linux/arm64" ]; then
    LAMBDA_ARCHITECTURE="arm64"
else
    LAMBDA_ARCHITECTURE="x86_64"
fi

# ==========================================
# 関数定義
# ==========================================

# ZIPファイルを作成する
create_lambda_zip() {
    echo "🔄 Creating zip file..."
    cd "$LAMBDA_BUILD_DIR"
    zip -r9 "$LAMBDA_ZIP_PATH" . -x "*/__pycache__/*" "*.pyc"
    cd - > /dev/null
    echo "✅ Zip file created: $LAMBDA_ZIP_PATH"
}

# Lambda関数をデプロイする汎用関数
deploy_lambda() {
    local function_name=$1
    local handler=$2
    local env_vars=$3
    local memory_size=${4:-1024}
    local timeout=${5:-900}

    echo "🚀 Deploying Lambda: $function_name..."

    if aws lambda get-function --function-name "$function_name" > /dev/null 2>&1; then
        echo "  → Function exists. Updating code and configuration..."

        # コードを更新
        aws lambda update-function-code \
            --function-name "$function_name" \
            --zip-file "fileb://$LAMBDA_ZIP_PATH" > /dev/null

        # 設定を更新
        aws lambda update-function-configuration \
            --function-name "$function_name" \
            --runtime python3.12 \
            --architectures "$LAMBDA_ARCHITECTURE" \
            --handler "$handler" \
            --memory-size "$memory_size" \
            --timeout "$timeout" \
            --environment "Variables={$env_vars}" > /dev/null

        echo "  ✅ Updated: $function_name"
    else
        echo "  → Function does not exist. Creating function..."

        aws lambda create-function \
            --function-name "$function_name" \
            --role arn:aws:iam::000000000000:role/dummy \
            --runtime python3.12 \
            --architectures "$LAMBDA_ARCHITECTURE" \
            --handler "$handler" \
            --zip-file "fileb://$LAMBDA_ZIP_PATH" \
            --memory-size "$memory_size" \
            --timeout "$timeout" \
            --environment "Variables={$env_vars}" > /dev/null

        echo "  ✅ Created: $function_name"
    fi
}

# Usage Event Consumer用の環境変数を構築
build_usage_event_consumer_env_vars() {
    echo "STAGE=$STAGE,
    SERVICE=$USAGE_EVENT_CONSUMER_NAME,
    APPLICATION_LOG_LEVEL=$APPLICATION_LOG_LEVEL,
    POSTGRES_HOST=$POSTGRES_HOST,
    POSTGRES_PORT=$POSTGRES_PORT,
    POSTGRES_SECRET_NAME=$POSTGRES_SECRET_NAME,
    POSTGRES_USER=$POSTGRES_USER,
    POSTGRES_PASSWORD=$POSTGRES_PASSWORD,
    POSTGRES_DB=$POSTGRES_DB,
    S3_ROLE_ARN=$S3_ROLE_ARN,
    USAGE_EVENT_STORAGE_NAME=$USAGE_EVENT_STORAGE_NAME,
    AWS_XRAY_SDK_ENABLED=false"
}

# ==========================================
# メイン処理
# ==========================================

# 1. ZIPファイル作成
create_lambda_zip

# 2. Lambda関数のデプロイ
echo "📦 Deploying Lambda functions..."

# Usage Event Consumer
USAGE_EVENT_ENV_VARS=$(build_usage_event_consumer_env_vars)
deploy_lambda \
    "$USAGE_EVENT_CONSUMER_NAME" \
    "src.usage_event_consumer.handler" \
    "$USAGE_EVENT_ENV_VARS" \
    1024 \
    900


echo "🎉 All deployments complete!"
