#!/usr/bin/env bash
#MISE description="Build and push image to local registry (DOCKERFILE=... NAME=... [TAG=dev])"
set -euo pipefail

if [ -z "${DOCKERFILE:-}" ]; then
  echo "Error: DOCKERFILE is required (e.g., DOCKERFILE=./examples/demo_api/Dockerfile)"
  exit 1
fi
if [ -z "${NAME:-}" ]; then
  echo "Error: NAME is required (e.g., NAME=demo_api)"
  exit 1
fi

TAG="${TAG:-dev}"
CONTEXT="${CONTEXT:-$(dirname "$DOCKERFILE")/}"
REGISTRY="$(kubectl get configmap local-registry-hosting -n kube-public -o jsonpath='{.data.localRegistryHosting\.v1}' 2>/dev/null | grep '^host:' | awk '{print $2}')"
if [ -z "$REGISTRY" ]; then
  echo "Error: Could not find local registry. Is minikube running with ctlptl?"
  exit 1
fi

echo "Building $NAME:$TAG from $DOCKERFILE (context: $CONTEXT)"
echo "Pushing to $REGISTRY/$NAME:$TAG"
docker build -t "$REGISTRY/$NAME:$TAG" -f "$DOCKERFILE" "$CONTEXT"
docker push "$REGISTRY/$NAME:$TAG"
echo ""
echo "Image pushed successfully!"
echo "Use in pod spec: ctlptl-registry:5000/$NAME:$TAG"
