#!/usr/bin/env bash
# Master script to verify all Ethereum Mainnet contracts
# Tries multiple verification methods automatically
# Usage: ./verify-all-mainnet-contracts.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'
CYAN='\033[0;36m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }
log_step() { echo -e "${CYAN}[STEP]${NC} $1"; }

log_info "========================================="
log_info "Ethereum Mainnet Contract Verification"
log_info "Automated Multi-Method Verification"
log_info "========================================="
log_info ""

# Method 1: Try Python script (most reliable for Standard JSON)
log_step "Method 1: Attempting verification with Python script (Standard JSON)..."
if command -v python3 &> /dev/null; then
    if python3 "$SCRIPT_DIR/verify-ethereum-mainnet.py"; then
        log_success "Verification completed successfully!"
        exit 0
    else
        log_warn "Python script verification failed or incomplete"
    fi
else
    log_warn "Python3 not available, skipping Python method"
fi

log_info ""

# Method 2: Try bash script with Standard JSON
log_step "Method 2: Attempting verification with bash script (Standard JSON)..."
if [ -f "$SCRIPT_DIR/verify-ethereum-mainnet-standard-json.sh" ]; then
    if bash "$SCRIPT_DIR/verify-ethereum-mainnet-standard-json.sh"; then
        log_success "Verification completed successfully!"
        exit 0
    else
        log_warn "Bash Standard JSON verification failed or incomplete"
    fi
else
    log_warn "Standard JSON bash script not found"
fi

log_info ""

# Method 3: Try comprehensive bash script
log_step "Method 3: Attempting verification with comprehensive bash script..."
if [ -f "$SCRIPT_DIR/verify-all-ethereum-mainnet-contracts.sh" ]; then
    if bash "$SCRIPT_DIR/verify-all-ethereum-mainnet-contracts.sh"; then
        log_success "Verification completed successfully!"
        exit 0
    else
        log_warn "Comprehensive bash script verification failed or incomplete"
    fi
else
    log_warn "Comprehensive bash script not found"
fi

log_info ""

# Method 4: Try original forge verification script
log_step "Method 4: Attempting verification with Forge (fallback)..."
if [ -f "$SCRIPT_DIR/verify-contract-etherscan.sh" ]; then
    if bash "$SCRIPT_DIR/verify-contract-etherscan.sh"; then
        log_success "Verification completed successfully!"
        exit 0
    else
        log_warn "Forge verification failed or incomplete"
    fi
else
    log_warn "Forge verification script not found"
fi

log_info ""
log_error "All automated verification methods failed or were unavailable"
log_info ""
log_info "Please verify contracts manually:"
log_info "1. Go to: https://etherscan.io/address/0x89dd12025bfcd38a168455a44b400e913ed33be2#code"
log_info "2. Click 'Contract' tab → 'Verify and Publish'"
log_info "3. Select 'Standard JSON Input'"
log_info "4. Upload: $PROJECT_ROOT/docs/CCIPWETH9Bridge_standard_json.json"
log_info "5. Enter constructor args: 0x00000000000000000000000080226fc0ee2b096224eeac085bb9a8cba1146f7d000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000514910771af9ca656af840dff83e8264ecf986ca"
log_info "6. Submit"
log_info ""

exit 1

