#!/bin/bash

# Script to tag and push all Bakery IA images to a container registry
# Usage: ./tag-and-push-images.sh [REGISTRY_PREFIX] [TAG]
# Example: ./tag-and-push-images.sh myuser/bakery v1.0.0

set -e

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

# Configuration
REGISTRY_PREFIX="${1:-}"
TAG="${2:-latest}"

if [ -z "$REGISTRY_PREFIX" ]; then
    echo -e "${RED}Error: Registry prefix required${NC}"
    echo "Usage: $0 REGISTRY_PREFIX [TAG]"
    echo ""
    echo "Examples:"
    echo "  Docker Hub:  $0 myusername/bakery v1.0.0"
    echo "  GitHub:      $0 ghcr.io/myorg/bakery v1.0.0"
    echo "  MicroK8s:    $0 YOUR_VPS_IP:32000/bakery v1.0.0"
    exit 1
fi

# List of all services
SERVICES=(
    "gateway"
    "dashboard"
    "auth-service"
    "tenant-service"
    "training-service"
    "forecasting-service"
    "sales-service"
    "external-service"
    "notification-service"
    "inventory-service"
    "recipes-service"
    "suppliers-service"
    "pos-service"
    "orders-service"
    "production-service"
    "procurement-service"
    "orchestrator-service"
    "alert-processor"
    "ai-insights-service"
    "demo-session-service"
    "distribution-service"
)

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Bakery IA - Image Tagging and Push${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo "Registry: $REGISTRY_PREFIX"
echo "Tag: $TAG"
echo ""

# Function to tag image
tag_image() {
    local service=$1
    local local_name="bakery/${service}"
    local remote_name="${REGISTRY_PREFIX}-${service}:${TAG}"

    echo -e "${YELLOW}Tagging ${local_name} -> ${remote_name}${NC}"

    if docker tag "$local_name" "$remote_name"; then
        echo -e "${GREEN}✓ Tagged $service${NC}"
        return 0
    else
        echo -e "${RED}✗ Failed to tag $service${NC}"
        return 1
    fi
}

# Function to push image
push_image() {
    local service=$1
    local remote_name="${REGISTRY_PREFIX}-${service}:${TAG}"

    echo -e "${YELLOW}Pushing ${remote_name}${NC}"

    if docker push "$remote_name"; then
        echo -e "${GREEN}✓ Pushed $service${NC}"
        return 0
    else
        echo -e "${RED}✗ Failed to push $service${NC}"
        return 1
    fi
}

# Check if user is logged in to registry
echo -e "${YELLOW}Checking registry authentication...${NC}"
if ! docker info > /dev/null 2>&1; then
    echo -e "${RED}Error: Docker daemon not running${NC}"
    exit 1
fi

echo -e "${GREEN}✓ Docker is running${NC}"
echo ""

# Ask for confirmation
echo -e "${YELLOW}This will tag and push ${#SERVICES[@]} images.${NC}"
read -p "Continue? (yes/no): " confirm

if [ "$confirm" != "yes" ]; then
    echo "Cancelled."
    exit 0
fi

echo ""
echo -e "${GREEN}Starting image tagging and push...${NC}"
echo ""

# Track success/failure
SUCCESS_COUNT=0
FAILED_SERVICES=()

# Tag and push all images
for service in "${SERVICES[@]}"; do
    if tag_image "$service" && push_image "$service"; then
        ((SUCCESS_COUNT++))
    else
        FAILED_SERVICES+=("$service")
    fi
    echo ""
done

# Summary
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Summary${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo "Successfully pushed: $SUCCESS_COUNT/${#SERVICES[@]}"

if [ ${#FAILED_SERVICES[@]} -gt 0 ]; then
    echo -e "${RED}Failed services:${NC}"
    for service in "${FAILED_SERVICES[@]}"; do
        echo -e "${RED}  - $service${NC}"
    done
    exit 1
else
    echo -e "${GREEN}All images pushed successfully!${NC}"
    echo ""
    echo "Next steps:"
    echo "1. Update image names in infrastructure/environments/prod/k8s-manifests/kustomization.yaml"
    echo "2. Deploy to production: kubectl apply -k infrastructure/environments/prod/k8s-manifests"
fi

echo ""
