#!/usr/bin/env bash
# Verify CCIP Sender Deployment
# Task 2: Verify Sender Deployment
# Usage: ./verify-ccip-sender.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}"
CCIP_SENDER="0x105F8A15b819948a89153505762444Ee9f324684"
CCIP_ROUTER="0x42DAb7b888Dd382bD5Adcf9E038dBF1fD03b4817"

log_info "========================================="
log_info "CCIP Sender Verification"
log_info "========================================="
log_info ""
log_info "Sender Address: $CCIP_SENDER"
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_SENDER" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -z "$BYTECODE" ] || [ "$BYTECODE" = "0x" ]; then
    log_error "Sender contract has no bytecode at address $CCIP_SENDER"
    exit 1
fi

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

# Step 2: Try to get Router address (if function exists)
log_info ""
log_info "Step 2: Testing Sender functions..."

log_info "  Testing router()..."
ROUTER_ADDR=$(cast call "$CCIP_SENDER" "router()" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$ROUTER_ADDR" ] && [ "$ROUTER_ADDR" != "0x" ]; then
    ROUTER_CLEAN=$(echo "$ROUTER_ADDR" | grep -oE "^0x[0-9a-fA-F]{40}$" | head -1 || echo "")
    if [ -n "$ROUTER_CLEAN" ]; then
        if [ "$(echo "$ROUTER_CLEAN" | tr '[:upper:]' '[:lower:]')" = "$(echo "$CCIP_ROUTER" | tr '[:upper:]' '[:lower:]')" ]; then
            log_success "  router(): $ROUTER_CLEAN (matches expected Router)"
        else
            log_warn "  router(): $ROUTER_CLEAN (different from expected: $CCIP_ROUTER)"
        fi
    else
        log_warn "  router(): Function returned invalid address"
    fi
else
    log_warn "  router(): Function not available or returned empty"
fi

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

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

