#!/usr/bin/env bash
# Build multi-arch production image and optionally push to Docker Hub.
# Usage:
#   ./scripts/release-cloud.sh              # build locally (current arch)
#   ./scripts/release-cloud.sh --push       # build+push linux/amd64,linux/arm64
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

IMAGE="${DOCKER_IMAGE:-atakanozban/songs2vid}"
TAG="${DOCKER_TAG:-latest}"
FULL="${IMAGE}:${TAG}"

echo "==> Prisma generate"
npx prisma generate

echo "==> Typecheck / lint (optional soft)"
npm run lint || true

echo "==> Watermark unit checks"
npx tsx scripts/watermark.test.ts

if [[ "${1:-}" == "--push" ]]; then
  echo "==> Multi-arch build+push ${FULL} (amd64,arm64)"
  docker buildx create --name s2yt-builder --use 2>/dev/null || docker buildx use s2yt-builder
  docker buildx build \
    --platform linux/amd64,linux/arm64 \
    -t "$FULL" \
    --push \
    .
  echo "Pushed ${FULL}. Deploy on songs2vid.com host with deploy/songs2vid/docker-compose.yml"
else
  echo "==> Local docker build ${FULL}"
  docker build -t "$FULL" .
  echo "Built ${FULL}. Re-run with --push for Hub multi-arch."
fi
