#!/bin/bash
# Build and Push Base Docker Images
# Usage: ./scripts/build-base-images.sh [registry] [version] [owner]
# Example: ./scripts/build-base-images.sh gitea.harkon.co.uk v1.0.1 harkon

set +e

# Configuration
REGISTRY="${1:-gitea.harkon.co.uk}"
VERSION="${2:-v1.0.1}"
OWNER="${3:-harkon}"

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

log_info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

log_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

log_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

log_error() {
    echo -e "${RED}❌ $1${NC}"
}

# Check if Docker is running
if ! docker info > /dev/null 2>&1; then
    log_error "Docker is not running. Please start Docker and try again."
    exit 1
fi

# Login to registry
log_info "Logging in to registry: $REGISTRY"
docker login $REGISTRY

echo ""
log_info "Building base images for AI Tax Agent"
log_info "Registry: $REGISTRY"
log_info "Owner: $OWNER"
log_info "Version: $VERSION"
echo ""

# Build base-runtime image
log_info "Building base-runtime image (core dependencies for all services)..."
BASE_RUNTIME_IMAGE="$REGISTRY/$OWNER/base-runtime:$VERSION"

if docker build \
    -t $BASE_RUNTIME_IMAGE \
    -f infra/docker/base-runtime.Dockerfile \
    --build-arg VERSION=$VERSION \
    . ; then
    
    log_success "Built: $BASE_RUNTIME_IMAGE"
    
    # Push image
    log_info "Pushing: $BASE_RUNTIME_IMAGE"
    if docker push $BASE_RUNTIME_IMAGE; then
        log_success "Pushed: $BASE_RUNTIME_IMAGE"
    else
        log_error "Failed to push: $BASE_RUNTIME_IMAGE"
    fi
    
    # Tag as latest
    LATEST_IMAGE="$REGISTRY/$OWNER/base-runtime:latest"
    docker tag $BASE_RUNTIME_IMAGE $LATEST_IMAGE
    if docker push $LATEST_IMAGE; then
        log_success "Also pushed as: $LATEST_IMAGE"
    fi
else
    log_error "Failed to build: $BASE_RUNTIME_IMAGE"
fi

echo ""

# Build base-ml image
log_info "Building base-ml image (ML dependencies - this will take 5-10 minutes)..."
BASE_ML_IMAGE="$REGISTRY/$OWNER/base-ml:$VERSION"

if docker build \
    -t $BASE_ML_IMAGE \
    -f infra/docker/base-ml.Dockerfile \
    --build-arg VERSION=$VERSION \
    . ; then
    
    log_success "Built: $BASE_ML_IMAGE"
    
    # Push image
    log_info "Pushing: $BASE_ML_IMAGE (this will take a few minutes)..."
    if docker push $BASE_ML_IMAGE; then
        log_success "Pushed: $BASE_ML_IMAGE"
    else
        log_error "Failed to push: $BASE_ML_IMAGE"
    fi
    
    # Tag as latest
    LATEST_IMAGE="$REGISTRY/$OWNER/base-ml:latest"
    docker tag $BASE_ML_IMAGE $LATEST_IMAGE
    if docker push $LATEST_IMAGE; then
        log_success "Also pushed as: $LATEST_IMAGE"
    fi
else
    log_error "Failed to build: $BASE_ML_IMAGE"
fi

echo ""
log_success "🎉 Base images built and pushed successfully!"
echo ""
echo "Summary:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "  $REGISTRY/$OWNER/base-runtime:$VERSION (~300MB)"
echo "  $REGISTRY/$OWNER/base-ml:$VERSION (~1.2GB)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
log_info "Next steps:"
echo "  1. Update ML service Dockerfiles to use base-ml image"
echo "  2. Update non-ML service Dockerfiles to use base-runtime image (optional)"
echo "  3. Rebuild services with: ./scripts/build-and-push-images.sh"
echo ""
log_info "Check image sizes:"
echo "  docker images | grep '$REGISTRY/$OWNER/base'"
echo ""

