#!/usr/bin/env bash
# Verify CCIP Router Deployment
# Task 1: Verify Router Deployment
# Usage: ./verify-ccip-router.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# 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 .env exists
if [ -f "$PROJECT_ROOT/.env" ]; then
    source "$PROJECT_ROOT/.env"
elif [ -f "$PROJECT_ROOT/../.env" ]; then
    source "$PROJECT_ROOT/../.env"
fi

# Configuration
RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
# Canonical Chain 138 router (relay path); legacy direct router: 0x8078A09637e47Fa5Ed34F626046Ea2094a5CDE5e
CCIP_ROUTER="0x42DAb7b888Dd382bD5Adcf9E038dBF1fD03b4817"

log_info "========================================="
log_info "CCIP Router Verification"
log_info "========================================="
log_info ""
log_info "Router Address: $CCIP_ROUTER"
log_info "RPC URL: $RPC_URL"
log_info ""

# Step 1: Check contract exists and has code
log_info "Step 1: Checking contract existence..."
BYTECODE=$(cast code "$CCIP_ROUTER" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -z "$BYTECODE" ] || [ "$BYTECODE" = "0x" ]; then
    log_error "Router contract has no bytecode at address $CCIP_ROUTER"
    exit 1
fi

BYTECODE_LENGTH=$(echo -n "$BYTECODE" | wc -c)
log_success "Router contract exists (bytecode length: $BYTECODE_LENGTH characters)"

# Step 2: Try to call common Router functions
log_info ""
log_info "Step 2: Testing Router functions..."

# Try to get fee token (if function exists)
log_info "  Testing getFeeToken()..."
FEE_TOKEN=$(cast call "$CCIP_ROUTER" "getFeeToken()" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$FEE_TOKEN" ] && [ "$FEE_TOKEN" != "0x" ]; then
    log_success "  getFeeToken(): $FEE_TOKEN"
else
    log_warn "  getFeeToken(): Function not available or returned empty"
fi

# Try to get supported tokens (if function exists)
log_info "  Testing getSupportedTokens()..."
SUPPORTED_TOKENS=$(cast call "$CCIP_ROUTER" "getSupportedTokens()" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$SUPPORTED_TOKENS" ] && [ "$SUPPORTED_TOKENS" != "0x" ]; then
    log_success "  getSupportedTokens(): Available"
else
    log_warn "  getSupportedTokens(): Function not available"
fi

# Step 3: Check if Router is responsive
log_info ""
log_info "Step 3: Checking Router responsiveness..."
BLOCK_NUMBER=$(cast block-number --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$BLOCK_NUMBER" ]; then
    log_success "Router is responsive (current block: $BLOCK_NUMBER)"
else
    log_error "Router is not responsive"
    exit 1
fi

# Summary
log_info ""
log_info "========================================="
log_info "Verification Summary"
log_info "========================================="
log_success "✓ Router contract deployed and functional"
log_info "  Address: $CCIP_ROUTER"
log_info "  Bytecode: Present ($BYTECODE_LENGTH characters)"
log_info "  Status: Operational"
log_info ""

