#!/bin/bash

# Deploy Backend Services
# This script deploys all backend services for the bridge system

set -e

echo "=== Deploying Backend Services ==="

# Load environment variables
if [ -f .env ]; then
    export $(cat .env | grep -v '^#' | xargs)
fi

# Check required environment variables
REQUIRED_VARS=(
    "ETHEREUM_MAINNET_RPC"
    "ENHANCED_SWAP_ROUTER"
    "BRIDGE_SWAP_COORDINATOR"
)

for var in "${REQUIRED_VARS[@]}"; do
    if [ -z "${!var}" ]; then
        echo "Error: $var is not set"
        exit 1
    fi
done

# Build and deploy services
SERVICES=(
    "liquidity-engine"
    "market-reporting"
    "bridge-reserve"
    "iso-currency"
)

for service in "${SERVICES[@]}"; do
    echo ""
    echo "--- Deploying $service ---"
    cd "services/$service"
    
    if [ -f "docker-compose.yml" ]; then
        docker-compose up -d --build
        echo "$service deployed successfully"
    else
        echo "Warning: docker-compose.yml not found for $service"
    fi
    
    cd ../..
done

echo ""
echo "=== Service Deployment Complete ==="
echo ""
echo "Services deployed:"
for service in "${SERVICES[@]}"; do
    echo "  - $service"
done
echo ""
echo "Check service status with: docker ps"

