#!/usr/bin/env bash
# Verify all Mainnet deployments

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../lib/init.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
# Load .env via dotenv (RPC CR/LF trim). Fallback: raw source.
if [[ -f "$SCRIPT_DIR/../lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1090
  source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "${PROJECT_ROOT:-$REPO_ROOT}"
elif [[ -n "${PROJECT_ROOT:-}" && -f "$PROJECT_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$PROJECT_ROOT/.env"
  set +a
elif [[ -n "${REPO_ROOT:-}" && -f "$REPO_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$REPO_ROOT/.env"
  set +a
fi


log_info "=== Verifying Mainnet Deployments ==="

# Load environment
if [ -f "$PROJECT_ROOT/.env" ]; then
    source "$PROJECT_ROOT/.env"
fi

MAINNET_RPC="${ETHEREUM_MAINNET_RPC:-https://eth.llamarpc.com}"

# Function to verify contract
verify_contract() {
    local address=$1
    local name=$2
    
    if [ -z "$address" ] || [ "$address" == "" ]; then
        log_error "❌ $name: Not deployed (no address)"
        return 1
    fi
    
    if [[ ! "$address" =~ ^0x[0-9a-fA-F]{40}$ ]]; then
        log_error "❌ $name: Invalid address format: $address"
        return 1
    fi
    
    # Check if contract has code
    CODE=$(cast code "$address" --rpc-url "$MAINNET_RPC" 2>/dev/null || echo "")
    if [ -z "$CODE" ] || [ "$CODE" == "0x" ]; then
        log_error "❌ $name: No code at $address"
        return 1
    else
        CODE_SIZE=$(echo -n "$CODE" | wc -c)
        log_success "✅ $name: Deployed at $address (code size: $((CODE_SIZE/2)) bytes)"
        return 0
    fi
}

log_info "Checking deployed contracts..."

# Check for addresses in .env
CCIP_LOGGER_ADDRESS=$(grep "CCIP_LOGGER_ETH_ADDRESS" "$PROJECT_ROOT/.env" 2>/dev/null | cut -d'=' -f2 | tr -d ' "' || echo "")
CCIPWETH9_ADDRESS=$(grep "CCIPWETH9_BRIDGE_MAINNET" "$PROJECT_ROOT/.env" 2>/dev/null | cut -d'=' -f2 | tr -d ' "' || echo "")
CCIPWETH10_ADDRESS=$(grep "CCIPWETH10_BRIDGE_MAINNET" "$PROJECT_ROOT/.env" 2>/dev/null | cut -d'=' -f2 | tr -d ' "' || echo "")

VERIFIED=0
FAILED=0

if [ -n "$CCIP_LOGGER_ADDRESS" ]; then
    verify_contract "$CCIP_LOGGER_ADDRESS" "CCIPLogger" && ((VERIFIED++)) || ((FAILED++))
else
    log_warn "⚠️  CCIPLogger: Address not found in .env"
    ((FAILED++))
fi

if [ -n "$CCIPWETH9_ADDRESS" ]; then
    verify_contract "$CCIPWETH9_ADDRESS" "CCIPWETH9Bridge" && ((VERIFIED++)) || ((FAILED++))
else
    log_warn "⚠️  CCIPWETH9Bridge: Address not found in .env"
    ((FAILED++))
fi

if [ -n "$CCIPWETH10_ADDRESS" ]; then
    verify_contract "$CCIPWETH10_ADDRESS" "CCIPWETH10Bridge" && ((VERIFIED++)) || ((FAILED++))
else
    log_warn "⚠️  CCIPWETH10Bridge: Address not found in .env"
    ((FAILED++))
fi

log_info "=== Verification Summary ==="
echo "  Verified: $VERIFIED"
echo "  Failed/Missing: $FAILED"

if [ $FAILED -eq 0 ]; then
    log_success "✅ All contracts verified!"
    exit 0
else
    log_error "❌ Some contracts not verified"
    exit 1
fi
