#!/bin/bash
# GitDataAI Docker Push Script

set -e

# Get version from Cargo.toml
PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
CARGO_VERSION=$(grep -m1 'version' "${PROJECT_ROOT}/Cargo.toml" | sed 's/.*"\(.*\)".*/\1/')

# Configuration
REGISTRY=${REGISTRY:-""}
TAG=${TAG:-"${CARGO_VERSION:-latest}"}

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

# Services to push
SERVICES=("gitdata" "email" "gitpod" "gitsync" "migrate" "web")

# Function to print colored output
log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Function to push a service
push_service() {
    local service=$1
    local image_name="gitdata-${service}"

    # Add registry prefix if set
    if [ -n "$REGISTRY" ]; then
        image_name="${REGISTRY}/${image_name}"
    fi

    log_info "Pushing ${service}..."

    # Check if image exists locally
    if ! docker image inspect "${image_name}:${TAG}" > /dev/null 2>&1; then
        log_error "Image not found: ${image_name}:${TAG}"
        log_error "Please build the image first with: ./build.sh ${service}"
        return 1
    fi

    docker push "${image_name}:${TAG}"

    log_info "Successfully pushed ${image_name}:${TAG}"
}

# Parse command line arguments
PUSH_SERVICES=()
PUSH_ALL=true

while [[ $# -gt 0 ]]; do
    case $1 in
        --tag|-t)
            TAG="$2"
            shift 2
            ;;
        --registry|-r)
            REGISTRY="$2"
            shift 2
            ;;
        --help|-h)
            echo "Usage: $0 [OPTIONS] [SERVICE...]"
            echo ""
            echo "Options:"
            echo "  -t, --tag TAG        Docker image tag (default: latest)"
            echo "  -r, --registry REG   Docker registry prefix (required)"
            echo "  -h, --help           Show this help message"
            echo ""
            echo "Services:"
            echo "  gitdata              Main API service"
            echo "  email                Email service"
            echo "  gitpod               GitPod service"
            echo "  gitsync              GitSync service"
            echo "  migrate              Database migration"
            echo "  web                  Web frontend"
            echo ""
            echo "Examples:"
            echo "  $0 -r registry.com                    # Push all services"
            echo "  $0 -r registry.com gitdata web        # Push specific services"
            echo "  $0 -r registry.com -t v1.0.0          # Push with custom tag"
            exit 0
            ;;
        *)
            PUSH_SERVICES+=("$1")
            PUSH_ALL=false
            shift
            ;;
    esac
done

# Validate registry
if [ -z "$REGISTRY" ]; then
    log_error "Registry is required. Use -r or --registry to specify."
    echo "Example: $0 -r registry.com"
    exit 1
fi

# Push services
log_info "Starting Docker push..."
log_info "Registry: ${REGISTRY}"
log_info "Tag: ${TAG}"

if [ "$PUSH_ALL" = true ]; then
    log_info "Pushing all services..."
    for service in "${SERVICES[@]}"; do
        push_service "$service"
    done
else
    log_info "Pushing specified services: ${PUSH_SERVICES[*]}"
    for service in "${PUSH_SERVICES[@]}"; do
        push_service "$service"
    done
fi

log_info "Push completed successfully!"