#!/bin/bash

# Phase 5: Initialize System
# This script initializes the bridge system configuration

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$PROJECT_ROOT"
source "$PROJECT_ROOT/scripts/load-env.sh" >/dev/null 2>&1 || true

echo "=== Phase 5: Initialize System ==="

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

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

echo ""
echo "--- Initializing System Configuration ---"

forge script script/bridge/trustless/InitializeBridgeSystem.s.sol:InitializeBridgeSystem \
    --rpc-url "$ETHEREUM_MAINNET_RPC" \
    --broadcast \
    --via-ir \
    --private-key "$PRIVATE_KEY"

echo ""
echo "=== Phase 5 Complete ==="
echo "System initialized and configured"
