#!/bin/bash

# Build and push multi-architecture Docker image to Harbor
# Supports: linux/amd64, linux/arm64, linux/arm/v7

set -e

REGISTRY="images.caffeinetux.com"
IMAGE_NAME="apps/neon-vortex"
TAG="${1:-latest}"
FULL_IMAGE="${REGISTRY}/${IMAGE_NAME}:${TAG}"

echo "Building multi-architecture image: ${FULL_IMAGE}"

# Check if buildx is available
if ! docker buildx version > /dev/null 2>&1; then
    echo "Error: docker buildx is not available"
    echo "Please enable BuildKit and buildx"
    exit 1
fi

# Create builder instance if it doesn't exist
if ! docker buildx inspect multiarch-builder > /dev/null 2>&1; then
    echo "Creating buildx builder instance..."
    docker buildx create --name multiarch-builder --use
    docker buildx inspect --bootstrap
else
    echo "Using existing buildx builder..."
    docker buildx use multiarch-builder
fi

# Build and push for multiple architectures
echo "Building and pushing for amd64, arm64, and arm/v7..."
cd htlm
docker buildx build \
    --platform linux/amd64,linux/arm64,linux/arm/v7 \
    --tag "${FULL_IMAGE}" \
    --push \
    .

echo ""
echo "✅ Successfully built and pushed: ${FULL_IMAGE}"
echo "Supported architectures: linux/amd64, linux/arm64, linux/arm/v7"
echo ""
echo "To deploy with Helm:"
echo "  helm install neon-vortex ./neon-vortex-chart"
