#!/bin/bash
set -e

IMAGE_BASE="us-central1-docker.pkg.dev/chalk-develop/interview-images/mountain-viewing"
IMAGE_TAG_LATEST="${IMAGE_BASE}:latest"
REMOTE_CONTEXT="remote"

echo "Building Docker image using remote context..."
echo "Tags: $IMAGE_TAG_LATEST"
echo "Context: $REMOTE_CONTEXT"

# Verify the remote context exists
if ! docker context inspect "$REMOTE_CONTEXT" &>/dev/null; then
    echo "Error: Docker context '$REMOTE_CONTEXT' not found"
    echo "Available contexts:"
    docker context ls
    exit 1
fi

# Build and load the image to the remote Docker daemon
# Using --context to target the remote Docker daemon directly
docker --context "$REMOTE_CONTEXT" buildx build \
    --tag "$IMAGE_TAG_LATEST" \
    --load \
    .

# Build and push the image to Google's artifact registry.
docker --context "$REMOTE_CONTEXT" buildx build \
    --tag "$IMAGE_TAG_LATEST" \
    --push \
    .

echo "Successfully built and loaded images to remote Docker daemon:"
echo "  - $IMAGE_TAG_LATEST"
echo "You can now use these images on the remote machine"
