#!/usr/bin/env bash
# Pre-Flight Check for CCIP Configuration
# Validates all prerequisites before configuration
# Usage: ./pre-flight-check.sh

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"
source "$PROJECT_ROOT/scripts/lib/ccip-destinations.sh"

# 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_explorer_runtime_env

# Configuration
RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"

PASSED=0
FAILED=0
WARNINGS=0

check_pass() {
    ((PASSED++)) || true
    log_success "$1"
}

check_fail() {
    ((FAILED++)) || true
    log_error "$1"
}

check_warn() {
    ((WARNINGS++)) || true
    log_warn "$1"
}

log_info "========================================="
log_info "CCIP Pre-Flight Check"
log_info "========================================="
log_info ""
log_info "RPC URL: $RPC_URL"
log_info ""

# Check 1: RPC Connectivity
log_info "1. RPC Connectivity"
BLOCK_NUMBER=$(cast block-number --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$BLOCK_NUMBER" ]; then
    check_pass "RPC accessible (block: $BLOCK_NUMBER)"
else
    check_fail "RPC not accessible"
    exit 1
fi

# Check 2: PRIVATE_KEY from .env
log_info ""
log_info "2. PRIVATE_KEY Configuration"
if [ -z "${PRIVATE_KEY:-}" ]; then
    check_fail "PRIVATE_KEY not available in effective environment"
    log_info "  Export PRIVATE_KEY=0x... before running"
else
    check_pass "PRIVATE_KEY available in effective environment"
    
    # Validate private key format
    if ! echo "$PRIVATE_KEY" | grep -qE "^0x[0-9a-fA-F]{64}$"; then
        check_warn "PRIVATE_KEY format may be invalid (should be 0x followed by 64 hex chars)"
    else
        check_pass "PRIVATE_KEY format valid"
    fi
fi

# Check 3: Account from PRIVATE_KEY
log_info ""
log_info "3. Account Validation"
if [ -n "${PRIVATE_KEY:-}" ]; then
    ACCOUNT=$(cast wallet address "$PRIVATE_KEY" 2>/dev/null || echo "")
    if [ -z "$ACCOUNT" ]; then
        check_fail "Could not derive address from PRIVATE_KEY"
    else
        check_pass "Account address: $ACCOUNT"
        
        # Check ETH balance
        ETH_BALANCE=$(cast balance "$ACCOUNT" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
        ETH_BALANCE_ETH=$(cast --from-wei "$ETH_BALANCE" ether 2>/dev/null || echo "0")
        
        # Estimate required ETH (gas for ~14 transactions: 7 WETH9 + 7 WETH10)
        # Each transaction ~0.001 ETH, so need ~0.014 ETH minimum
        REQUIRED_ETH="0.02"
        
        if (( $(echo "$ETH_BALANCE_ETH >= $REQUIRED_ETH" | bc -l 2>/dev/null || echo 0) )); then
            check_pass "ETH balance sufficient: $ETH_BALANCE_ETH ETH (need $REQUIRED_ETH ETH)"
        else
            check_fail "Insufficient ETH balance: $ETH_BALANCE_ETH ETH (need at least $REQUIRED_ETH ETH)"
        fi
    fi
else
    check_fail "Cannot validate account (PRIVATE_KEY not set)"
fi

# Check 4: Current Nonce
log_info ""
log_info "4. Transaction Nonce"
if [ -n "${ACCOUNT:-}" ]; then
    CURRENT_NONCE=$(cast nonce "$ACCOUNT" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    if [ -n "$CURRENT_NONCE" ]; then
        check_pass "Current nonce: $CURRENT_NONCE"
        log_info "  Next available nonce: $CURRENT_NONCE"
    else
        check_fail "Could not get current nonce"
    fi
else
    check_fail "Cannot check nonce (account not available)"
fi

# Check 5: Contract Deployments
log_info ""
log_info "5. Contract Deployments"

ROUTER="$(resolve_address_value CCIP_ROUTER_ADDRESS CCIP_ROUTER_ADDRESS 0x42DAb7b888Dd382bD5Adcf9E038dBF1fD03b4817)"
SENDER="0x105F8A15b819948a89153505762444Ee9f324684"
WETH9_BRIDGE="$(resolve_address_value CCIPWETH9_BRIDGE CCIPWETH9_BRIDGE 0xcacfd227A040002e49e2e01626363071324f820a)"
WETH10_BRIDGE="$(resolve_address_value CCIPWETH10_BRIDGE CCIPWETH10_BRIDGE 0xe0E93247376aa097dB308B92e6Ba36bA015535D0)"

ROUTER_BYTECODE=$(cast code "$ROUTER" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$ROUTER_BYTECODE" ] && [ "$ROUTER_BYTECODE" != "0x" ]; then
    check_pass "Router deployed"
else
    check_fail "Router not deployed"
fi

SENDER_BYTECODE=$(cast code "$SENDER" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$SENDER_BYTECODE" ] && [ "$SENDER_BYTECODE" != "0x" ]; then
    check_pass "Sender deployed"
else
    check_fail "Sender not deployed"
fi

WETH9_BRIDGE_BYTECODE=$(cast code "$WETH9_BRIDGE" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$WETH9_BRIDGE_BYTECODE" ] && [ "$WETH9_BRIDGE_BYTECODE" != "0x" ]; then
    check_pass "WETH9 Bridge deployed"
else
    check_fail "WETH9 Bridge not deployed"
fi

WETH10_BRIDGE_BYTECODE=$(cast code "$WETH10_BRIDGE" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$WETH10_BRIDGE_BYTECODE" ] && [ "$WETH10_BRIDGE_BYTECODE" != "0x" ]; then
    check_pass "WETH10 Bridge deployed"
else
    check_fail "WETH10 Bridge not deployed"
fi

# Check 6: Destination Addresses
log_info ""
log_info "6. Destination Address Validation"

VALID_DESTINATIONS=0
TOTAL_DESTINATIONS="$(ccip_destination_count)"
while IFS=$'\t' read -r _chain_name SELECTOR WETH9_ADDR WETH10_ADDR _rpc_url; do
    if echo "$WETH9_ADDR" | grep -qE "^0x[0-9a-fA-F]{40}$" && \
       echo "$WETH10_ADDR" | grep -qE "^0x[0-9a-fA-F]{40}$"; then
        ((VALID_DESTINATIONS++)) || true
    fi
done < <(ccip_destination_rows)

if [ "$VALID_DESTINATIONS" -eq "$TOTAL_DESTINATIONS" ]; then
    check_pass "All $TOTAL_DESTINATIONS destination addresses valid"
else
    check_fail "Some destination addresses invalid ($VALID_DESTINATIONS/$TOTAL_DESTINATIONS valid)"
fi

# Check 7: Current Configuration Status
log_info ""
log_info "7. Current Configuration Status"

WETH9_CONFIGURED=0
WETH10_CONFIGURED=0

while IFS=$'\t' read -r _chain_name SELECTOR _weth9 _weth10 _rpc_url; do
    DEST_WETH9=$(cast call "$WETH9_BRIDGE" "destinations(uint64)" "$SELECTOR" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    DEST_WETH9_CLEAN=$(echo "$DEST_WETH9" | grep -oE "^0x[0-9a-fA-F]{40}$" | head -1 || echo "")
    if [ -n "$DEST_WETH9_CLEAN" ] && ! echo "$DEST_WETH9_CLEAN" | grep -qE "^0x0+$"; then
        ((WETH9_CONFIGURED++)) || true
    fi
    
    DEST_WETH10=$(cast call "$WETH10_BRIDGE" "destinations(uint64)" "$SELECTOR" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    DEST_WETH10_CLEAN=$(echo "$DEST_WETH10" | grep -oE "^0x[0-9a-fA-F]{40}$" | head -1 || echo "")
    if [ -n "$DEST_WETH10_CLEAN" ] && ! echo "$DEST_WETH10_CLEAN" | grep -qE "^0x0+$"; then
        ((WETH10_CONFIGURED++)) || true
    fi
done < <(ccip_destination_rows)

log_info "  WETH9 Bridge: $WETH9_CONFIGURED/$TOTAL_DESTINATIONS destinations configured"
log_info "  WETH10 Bridge: $WETH10_CONFIGURED/$TOTAL_DESTINATIONS destinations configured"

if [ "$WETH9_CONFIGURED" -eq "$TOTAL_DESTINATIONS" ] && [ "$WETH10_CONFIGURED" -eq "$TOTAL_DESTINATIONS" ]; then
    check_pass "All destinations already configured"
elif [ $WETH9_CONFIGURED -gt 0 ] || [ $WETH10_CONFIGURED -gt 0 ]; then
    check_warn "Partial configuration ($WETH9_CONFIGURED WETH9, $WETH10_CONFIGURED WETH10)"
else
    check_warn "No destinations configured (ready for configuration)"
fi

# Summary
log_info ""
log_info "========================================="
log_info "Pre-Flight Check Summary"
log_info "========================================="
log_info ""
log_success "Passed: $PASSED"
log_warn "Warnings: $WARNINGS"
log_error "Failed: $FAILED"
log_info ""

if [ $FAILED -eq 0 ]; then
    if [ $WARNINGS -eq 0 ]; then
        log_success "✓ All checks passed - Ready for configuration!"
        exit 0
    else
        log_warn "⚠ Checks passed with warnings - Configuration can proceed"
        exit 0
    fi
else
    log_error "✗ Some checks failed - Fix issues before configuration"
    log_info ""
    log_info "Next steps:"
    if [ -z "${PRIVATE_KEY:-}" ]; then
        log_info "  1. Export PRIVATE_KEY before rerunning"
    fi
    if [ -n "${ACCOUNT:-}" ] && [ -n "${ETH_BALANCE_ETH:-}" ]; then
        if (( $(echo "$ETH_BALANCE_ETH < 0.02" | bc -l 2>/dev/null || echo 1) )); then
            log_info "  2. Add ETH to account: $ACCOUNT"
        fi
    fi
    exit 1
fi
