#!/usr/bin/env bash
# Complete bridge setup - checks and configures everything
# This is a master script that orchestrates all bridge setup steps
# Usage: ./setup-complete-bridge.sh [private_key] [weth9_eth_mainnet] [weth10_eth_mainnet]

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "$PROJECT_ROOT/scripts/lib/address-inventory.sh"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
MAGENTA='\033[0;35m'
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"; }
log_step() { echo -e "${CYAN}[STEP]${NC} $1"; }
log_header() { echo -e "${MAGENTA}=========================================${NC}"; }

load_explorer_runtime_env

# Parse arguments
PRIVATE_KEY_ARG="${1:-}"
WETH9_ETH_MAINNET="${2:-}"
WETH10_ETH_MAINNET="${3:-}"

log_header
log_info "Complete Bridge Setup"
log_header
log_info ""

# Step 1: Check prerequisites
log_step "Step 1: Checking Prerequisites"
log_info ""

if [ -n "$PRIVATE_KEY_ARG" ]; then
    PRIVATE_KEY="$PRIVATE_KEY_ARG"
elif [ -z "${PRIVATE_KEY:-}" ]; then
    log_error "PRIVATE_KEY not provided"
    log_info "Usage: $0 [private_key] [weth9_eth_mainnet] [weth10_eth_mainnet]"
    log_info "Or export PRIVATE_KEY before running"
    exit 1
fi

DEPLOYER=$(cast wallet address --private-key "$PRIVATE_KEY" 2>/dev/null || echo "")
if [ -z "$DEPLOYER" ]; then
    log_error "Failed to get address from private key"
    exit 1
fi

log_success "✓ Private key valid"
log_info "  Address: $DEPLOYER"
log_info ""

# Step 2: Check WETH9/WETH10 contracts
log_step "Step 2: Checking WETH9 and WETH10 Contracts"
log_info ""

if "$SCRIPT_DIR/inspect-weth9-contract.sh" > /dev/null 2>&1; then
    log_success "✓ WETH9 contract verified"
else
    log_warn "⚠ WETH9 contract check had issues"
fi

if "$SCRIPT_DIR/inspect-weth10-contract.sh" > /dev/null 2>&1; then
    log_success "✓ WETH10 contract verified"
else
    log_warn "⚠ WETH10 contract check had issues"
fi

log_info ""

# Step 3: Check bridge configuration
log_step "Step 3: Checking Bridge Configuration"
log_info ""

BRIDGE_STATUS=$("$SCRIPT_DIR/check-bridge-config.sh" 2>&1 | grep -E "(configured|missing)" | head -4 || echo "")

if echo "$BRIDGE_STATUS" | grep -q "0 configured"; then
    log_warn "⚠ No destinations configured"
    NEEDS_CONFIG=true
else
    log_info "Some destinations may be configured"
    NEEDS_CONFIG=true  # Still check and configure missing ones
fi

log_info ""

# Step 4: Configure all bridge destinations
log_step "Step 4: Configuring Bridge Destinations"
log_info ""

if [ "$NEEDS_CONFIG" = true ]; then
    log_info "Running bridge configuration..."
    
    if [ -n "$WETH9_ETH_MAINNET" ] && [ -n "$WETH10_ETH_MAINNET" ]; then
        "$SCRIPT_DIR/configure-all-bridge-destinations.sh" "$PRIVATE_KEY" "$WETH9_ETH_MAINNET" "$WETH10_ETH_MAINNET"
    else
        "$SCRIPT_DIR/configure-all-bridge-destinations.sh" "$PRIVATE_KEY"
    fi
    
    CONFIG_RESULT=$?
    if [ $CONFIG_RESULT -eq 0 ]; then
        log_success "✓ Bridge configuration completed"
    else
        log_warn "⚠ Some configurations may have failed"
    fi
else
    log_info "Skipping - bridges already configured"
fi

log_info ""

# Step 5: Verify configuration
log_step "Step 5: Verifying Configuration"
log_info ""

"$SCRIPT_DIR/check-bridge-config.sh" 2>&1 | tail -10

log_info ""

# Step 6: Run dry run test
log_step "Step 6: Running Dry Run Test"
log_info ""

log_info "Testing bridge to Ethereum Mainnet with 0.1 ETH..."
"$SCRIPT_DIR/dry-run-bridge-to-ethereum.sh" 0.1 "$DEPLOYER" 2>&1 | tail -20

log_info ""

# Step 7: Summary
log_header
log_info "Setup Summary"
log_header
log_info ""

log_info "✅ Prerequisites: Verified"
log_info "✅ Contracts: Checked"
log_info "✅ Bridge Configuration: Completed"
log_info "✅ Verification: Done"
log_info "✅ Dry Run: Completed"
log_info ""

log_success "Bridge setup complete!"
log_info ""
log_info "Next steps:"
log_info "  1. Review dry run results above"
log_info "  2. If all checks pass, bridge tokens:"
log_info "     ./scripts/wrap-and-bridge-to-ethereum.sh [amount] [private_key]"
log_info ""
