#!/bin/bash
set -euo pipefail

REGISTRY="forgejo.riotpiao.com"
IMAGE_NAME="rock/claude-terminal"
TAG="latest"
FULL_IMAGE="${REGISTRY}/${IMAGE_NAME}:${TAG}"

echo "🔨 Building Claude Terminal image for linux/amd64..."
docker buildx build --platform linux/amd64 \
  -t "${FULL_IMAGE}" \
  -f Dockerfile \
  . || { echo "❌ Build failed"; exit 1; }

echo "🔓 Logging in to Forgejo registry..."
REGISTRY_TOKEN=$(talos get cluster/iam/agents/ci-bot --key token)
echo "${REGISTRY_TOKEN}" | docker login "${REGISTRY}" \
  --username ci-bot \
  --password-stdin || { echo "❌ Login failed"; exit 1; }

echo "📤 Pushing image to registry..."
docker push "${FULL_IMAGE}" || { echo "❌ Push failed"; exit 1; }

echo "✅ Successfully pushed ${FULL_IMAGE}"
