#!/usr/bin/env bash
# Complete bridge configuration for all networks
# Usage: ./complete-bridge-configuration.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
SOURCE_PROJECT="/home/intlc/projects/smom-dbis-138"

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

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

# Load environment variables
if [ -f "$SOURCE_PROJECT/.env" ]; then
    source "$SOURCE_PROJECT/.env"
else
    log_error ".env file not found in $SOURCE_PROJECT"
    exit 1
fi

log_info "========================================="
log_info "Complete Bridge Configuration"
log_info "========================================="
log_info ""

# Step 1: Configure Chain 138 bridges
log_info "Step 1: Configuring Chain 138 bridge destinations..."
if [ -f "$PROJECT_ROOT/scripts/configure-bridge-destinations.sh" ]; then
    cd "$PROJECT_ROOT"
    if AUTO_DEPLOY=yes bash scripts/configure-bridge-destinations.sh 2>&1 | grep -E "(✓|✗|ERROR)" | tail -20; then
        log_success "✓ Chain 138 bridge configuration complete"
    else
        log_warn "⚠️  Chain 138 configuration may have issues - check logs"
    fi
else
    log_warn "Chain 138 configuration script not found"
fi

log_info ""

# Step 2: Configure Ethereum Mainnet bridges
log_info "Step 2: Configuring Ethereum Mainnet bridge destinations..."
if [ -f "$PROJECT_ROOT/scripts/configure-ethereum-mainnet-bridge-destinations.sh" ]; then
    cd "$PROJECT_ROOT"
    if AUTO_DEPLOY=yes bash scripts/configure-ethereum-mainnet-bridge-destinations.sh 2>&1 | grep -E "(✓|✗|ERROR)" | tail -20; then
        log_success "✓ Ethereum Mainnet bridge configuration complete"
    else
        log_warn "⚠️  Ethereum Mainnet configuration may have issues - check logs"
    fi
else
    log_warn "Ethereum Mainnet configuration script not found"
fi

log_info ""
log_success "========================================="
log_success "Bridge Configuration Complete!"
log_success "========================================="

