#!/bin/bash

# Remote Build Script for base-ml Image
# This script builds the base-ml image on the remote production server
# to avoid pushing 1.2GB+ over the network from local machine

set -e

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

# Logging functions
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}"
}

# Configuration
REMOTE_HOST="${1:-deploy@141.136.35.199}"
REMOTE_DIR="${2:-/home/deploy/ai-tax-agent}"
REGISTRY="${3:-gitea.harkon.co.uk}"
VERSION="${4:-v1.0.1}"
OWNER="${5:-harkon}"

log_info "Remote Build Configuration"
echo "  Remote Host: $REMOTE_HOST"
echo "  Remote Directory: $REMOTE_DIR"
echo "  Registry: $REGISTRY"
echo "  Owner: $OWNER"
echo "  Version: $VERSION"
echo ""

# Step 1: Check if remote directory exists
log_info "Checking remote directory..."
if ! ssh "$REMOTE_HOST" "[ -d $REMOTE_DIR ]"; then
    log_error "Remote directory $REMOTE_DIR does not exist!"
    log_info "Creating remote directory..."
    ssh "$REMOTE_HOST" "mkdir -p $REMOTE_DIR"
fi
log_success "Remote directory exists"

# Step 2: Sync code to remote server
log_info "Syncing code to remote server..."
rsync -avz --exclude='.git' \
    --exclude='__pycache__' \
    --exclude='*.pyc' \
    --exclude='.venv' \
    --exclude='venv' \
    --exclude='node_modules' \
    --exclude='.pytest_cache' \
    --exclude='.mypy_cache' \
    --exclude='.ruff_cache' \
    --exclude='*.egg-info' \
    --exclude='.DS_Store' \
    ./ "$REMOTE_HOST:$REMOTE_DIR/"
log_success "Code synced to remote server"

# Step 3: Build base-ml on remote
log_info "Building base-ml image on remote server..."
log_warning "This will take 10-15 minutes (installing ML dependencies)..."

ssh "$REMOTE_HOST" << 'ENDSSH'
    set -e
    cd /home/deploy/ai-tax-agent

    # Build base-ml image
    echo "Building base-ml image..."
    docker build \
        -f infra/docker/base-ml.Dockerfile \
        -t gitea.harkon.co.uk/harkon/base-ml:v1.0.1 \
        -t gitea.harkon.co.uk/harkon/base-ml:latest \
        .

    # Push to registry
    echo "Pushing base-ml image to registry..."
    docker push gitea.harkon.co.uk/harkon/base-ml:v1.0.1
    docker push gitea.harkon.co.uk/harkon/base-ml:latest

    # Show image size
    echo ""
    echo "=== Base ML Image Built ==="
    docker images | grep "base-ml"
    echo ""
ENDSSH

log_success "base-ml image built and pushed from remote server!"

# Step 4: Verify image is available
log_info "Verifying image is available in registry..."
log_info "You can check at: https://$REGISTRY/$OWNER/-/packages/container/base-ml"

log_success "Done! base-ml image is ready to use."
log_info "Next steps:"
echo "  1. Pull base-ml locally (optional): docker pull $REGISTRY/$OWNER/base-ml:$VERSION"
echo "  2. Build ML services: ./scripts/build-and-push-images.sh $REGISTRY $VERSION $OWNER"
