#!/bin/bash

# Exit script on any error
set -e

# Enable verbose mode for debugging
set -x

# --- Configuration ---
# Get the script's directory for absolute paths
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_ROOT="$( cd "$SCRIPT_DIR/.." && pwd )"

# Path to the Dockerfile using absolute path
DOCKERFILE_PATH="$PROJECT_ROOT/src/boxpwnr/executors/docker/Dockerfile"
# File to store the hash of the last successfully pushed Dockerfile
LAST_HASH_FILE="$SCRIPT_DIR/.last_docker_hash"
# Terraform directory using absolute path
TERRAFORM_DIR="$SCRIPT_DIR/infra"
# AWS Region (Ensure this matches your Terraform config and ECR repo)
AWS_REGION="us-east-1"
# Platform for the build (optional, adjust if needed e.g., linux/amd64)
PLATFORM_FLAG="--platform linux/amd64"

# Print path information for debugging
echo "Script directory: $SCRIPT_DIR"
echo "Project root: $PROJECT_ROOT"
echo "Dockerfile path: $DOCKERFILE_PATH"
echo "Terraform directory: $TERRAFORM_DIR"

# Better error handling
function handle_error {
    echo "ERROR: An error occurred at line $1"
    exit 1
}

# Set up error trap
trap 'handle_error $LINENO' ERR

# --- Calculate Hashes ---
echo "Calculating hash for $DOCKERFILE_PATH..."
if [ ! -f "$DOCKERFILE_PATH" ]; then
    echo "Error: Dockerfile not found at $DOCKERFILE_PATH"
    exit 1
fi
# Include platform flag in hash calculation to ensure rebuilds when platform changes
DOCKERFILE_HASH=$(md5sum "$DOCKERFILE_PATH" | awk '{ print $1 }')
PLATFORM_HASH=$(echo "$PLATFORM_FLAG" | md5sum | awk '{ print $1 }')
CURRENT_HASH="${DOCKERFILE_HASH}_${PLATFORM_HASH}"
echo "Current Dockerfile hash: $DOCKERFILE_HASH"
echo "Platform flag hash: $PLATFORM_HASH"
echo "Combined hash: $CURRENT_HASH"

LAST_HASH=""
if [ -f "$LAST_HASH_FILE" ]; then
    LAST_HASH=$(cat "$LAST_HASH_FILE")
    echo "Last pushed hash: $LAST_HASH"
else
    echo "No last hash file found ($LAST_HASH_FILE). Will build and push."
fi

# --- Check if Build/Push is Needed ---
if [ "$CURRENT_HASH" == "$LAST_HASH" ]; then
    echo "Dockerfile hash hasn't changed. Image is up-to-date in ECR (assuming last push succeeded)."
    # Output the current hash for reference
    echo "Current hash: $CURRENT_HASH"
    exit 0
fi

echo "Dockerfile has changed or this is the first push. Building and pushing image..."

# --- Get ECR Repo URL from environment variable or Terraform output ---
echo "Getting ECR repository URL..."

# First check if environment variable is set
if [ ! -z "$ECR_REPO_URL" ]; then
    echo "Found ECR repository URL in environment variable: $ECR_REPO_URL"
else
    # Fall back to Terraform output if environment variable is not set
    echo "No ECR_REPO_URL environment variable found, trying Terraform output..."
    ECR_REPO_URL=$(cd "$TERRAFORM_DIR" && terraform output -raw ecr_repository_url)
    if [ -z "$ECR_REPO_URL" ]; then
        echo "Error: Failed to get ECR repository URL from Terraform output."
        echo "Make sure you have run 'terraform apply' successfully in the '$TERRAFORM_DIR' directory."
        echo "Or set the ECR_REPO_URL environment variable."
        exit 1
    fi
fi

echo "ECR Repository URL: $ECR_REPO_URL"

# --- Docker Login to ECR ---
echo "Logging in to AWS ECR ($AWS_REGION)..."
aws ecr get-login-password --region "$AWS_REGION" | docker login --username AWS --password-stdin "$ECR_REPO_URL"
echo "ECR login successful."

# --- Docker Build ---
# The context for the build is the directory containing the Dockerfile
BUILD_CONTEXT=$(dirname "$DOCKERFILE_PATH")
IMAGE_TAG_HASH="$ECR_REPO_URL:$DOCKERFILE_HASH"
IMAGE_TAG_LATEST="$ECR_REPO_URL:latest"

echo "Building Docker image ($PLATFORM_FLAG)..."
echo "Context: $BUILD_CONTEXT"
echo "Tags: $IMAGE_TAG_HASH, $IMAGE_TAG_LATEST"

docker build $PLATFORM_FLAG -t "$IMAGE_TAG_HASH" -t "$IMAGE_TAG_LATEST" -f "$DOCKERFILE_PATH" "$BUILD_CONTEXT"

echo "Build complete."

# --- Docker Push ---
echo "Pushing image tag: $IMAGE_TAG_HASH"
docker push "$IMAGE_TAG_HASH"

echo "Pushing image tag: $IMAGE_TAG_LATEST"
docker push "$IMAGE_TAG_LATEST"

echo "Push complete."

# --- Update Last Hash File ---
echo "Updating last hash file: $LAST_HASH_FILE"
echo -n "$CURRENT_HASH" > "$LAST_HASH_FILE"

echo "-------------------------------------"
echo "Docker image build and push process completed successfully."
echo "Image pushed with tag (hash): $DOCKERFILE_HASH"
echo "Platform: $(echo $PLATFORM_FLAG | cut -d= -f2)"
echo "-------------------------------------" 