#!/bin/bash

# Adopt-a-Street Multi-Arch Docker Build Script
# Builds images for ARM64 (Pi 5) and ARMv7 (Pi 3B+)

set -e  # Exit on error

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Configuration
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
REGISTRY="${DOCKER_REGISTRY:-your-registry}"
TAG="${DOCKER_TAG:-latest}"

echo -e "${GREEN}🐳 Adopt-a-Street Multi-Arch Docker Build${NC}"
echo "================================================"
echo "Registry: ${REGISTRY}"
echo "Tag: ${TAG}"
echo "Project Root: ${PROJECT_ROOT}"
echo ""

# Check if docker buildx is available
if ! docker buildx version &> /dev/null; then
    echo -e "${RED}❌ Docker buildx not found. Please install Docker with buildx support.${NC}"
    exit 1
fi

# Create buildx builder if it doesn't exist
if ! docker buildx inspect multiarch-builder &> /dev/null; then
    echo "🔨 Creating buildx builder..."
    docker buildx create --use --name multiarch-builder
    echo -e "${GREEN}✓${NC} Builder created"
else
    echo "🔨 Using existing buildx builder..."
    docker buildx use multiarch-builder
fi
echo ""

# Prompt for registry if using default
if [ "${REGISTRY}" = "your-registry" ]; then
    echo -e "${YELLOW}⚠️  Using default registry 'your-registry'${NC}"
    echo -e "${YELLOW}Set DOCKER_REGISTRY environment variable to use a different registry:${NC}"
    echo "  export DOCKER_REGISTRY=docker.io/username"
    echo "  export DOCKER_REGISTRY=ghcr.io/username"
    echo ""
    read -p "Continue with 'your-registry'? (y/N) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "Aborted."
        exit 1
    fi
fi

# Build backend
echo "🔧 Building backend image..."
echo "  Platforms: linux/arm64, linux/arm/v7"
echo "  Image: ${REGISTRY}/adopt-a-street-backend:${TAG}"
docker buildx build --platform linux/arm64,linux/arm/v7 \
    -t "${REGISTRY}/adopt-a-street-backend:${TAG}" \
    --push \
    "${PROJECT_ROOT}/backend"
echo -e "${GREEN}✓${NC} Backend image built and pushed"
echo ""

# Build frontend
echo "🎨 Building frontend image..."
echo "  Platforms: linux/arm64, linux/arm/v7"
echo "  Image: ${REGISTRY}/adopt-a-street-frontend:${TAG}"
docker buildx build --platform linux/arm64,linux/arm/v7 \
    -t "${REGISTRY}/adopt-a-street-frontend:${TAG}" \
    --push \
    "${PROJECT_ROOT}/frontend"
echo -e "${GREEN}✓${NC} Frontend image built and pushed"
echo ""

echo "================================================"
echo -e "${GREEN}✅ Build Complete!${NC}"
echo "================================================"
echo ""
echo "Images pushed:"
echo "  📦 ${REGISTRY}/adopt-a-street-backend:${TAG}"
echo "  📦 ${REGISTRY}/adopt-a-street-frontend:${TAG}"
echo ""
echo -e "${YELLOW}📝 Next Steps:${NC}"
echo "1. Update image references in deployment files:"
echo "   sed -i 's|your-registry|${REGISTRY}|g' deploy/k8s/*.yaml"
echo ""
echo "2. Deploy to Kubernetes:"
echo "   ./deploy/scripts/deploy.sh"
echo ""
echo -e "${GREEN}🎉 Happy deploying!${NC}"
