#!/bin/bash

# --- Configuration ---
IMAGE_NAME="workit"
TAG="latest"
DEV_BUILD_DIR="public_dev"

# --- Script Start ---
echo "--- Preparing development build directory ---"
# Clean up previous development build directory if it exists
rm -rf ${DEV_BUILD_DIR}
# Create a temporary build directory for development
cp -r public ${DEV_BUILD_DIR}

# Step 1: Run the Node.js script to inline audio files.
echo ""
echo "--- Inlining audio files ---"
node scripts/inline-audio.js

# Check if the node script was successful.
if [ $? -ne 0 ]; then
    echo "!!! Audio inlining failed. Aborting development build."
    rm -rf ${DEV_BUILD_DIR} # Clean up
    exit 1
fi

echo ""
echo "--- Starting Docker image build for development ---"

# Step 2: Build the image for current architecture (not hardcoded to amd64)
# This will automatically detect the current platform for local development
docker build -t ${IMAGE_NAME}:${TAG} .

# Check if the build command was successful.
if [ $? -ne 0 ]; then
    echo "!!! Docker build failed. Aborting script."
    rm -rf ${DEV_BUILD_DIR} # Clean up
    exit 1
fi

# Step 3: Clean up the temporary development build directory
echo ""
echo "--- Cleaning up development build directory ---"
rm -rf ${DEV_BUILD_DIR}

echo ""
echo "✅ Development build complete!"
echo "To run the container:"
echo "  docker run -p 8080:80 ${IMAGE_NAME}:${TAG}"
echo ""
echo "Or with docker-compose:"
echo "  docker-compose up"