#!/bin/bash

# Build and push Docker image to Local Docker Registry
# Usage: ./build-and-push.sh [VERSION_TAG]

set -e

# Configuration
LOCAL_REGISTRY="192.168.0.107:5000"
REPO="owi-cms"
IMAGE_NAME="$LOCAL_REGISTRY/$REPO"

# Get version tag (default to 'latest')
VERSION_TAG="${1:-latest}"
FULL_IMAGE="$IMAGE_NAME:$VERSION_TAG"

echo "=========================================="
echo "Building Docker image: $FULL_IMAGE"
echo "=========================================="

# Build the Docker image
docker build \
  --tag "$FULL_IMAGE" \
  --tag "$IMAGE_NAME:latest" \
  --file Dockerfile \
  .

if [ $? -ne 0 ]; then
  echo "❌ Build failed!"
  exit 1
fi

echo ""
echo "✅ Build successful!"
echo ""
echo "=========================================="
echo "Next steps:"
echo "=========================================="
echo ""
echo "1. Push to Local Docker Registry:"
echo "   docker push $FULL_IMAGE"
echo "   docker push $IMAGE_NAME:latest"
echo ""
echo "2. Update Dockge on TrueNAS to pull the new image:"
echo "   Image: $FULL_IMAGE"
echo "   Registry: $GITHUB_REGISTRY"
echo ""
