#!/usr/bin/env bash
# Automated script to verify all Ethereum Mainnet contracts
# This script handles bytecode mismatch issues and uses multiple verification methods
# Usage: ./verify-all-ethereum-mainnet-contracts.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
SOURCE_PROJECT="/home/intlc/projects/smom-dbis-138"
DOCS_DIR="$PROJECT_ROOT/docs"

# 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"; }

# Load environment
if [ -f "$SOURCE_PROJECT/.env" ]; then
    source "$SOURCE_PROJECT/.env"
else
    log_error ".env file not found in $SOURCE_PROJECT"
    exit 1
fi

# Required environment variables
if [ -z "${ETHERSCAN_API_KEY:-}" ]; then
    log_error "ETHERSCAN_API_KEY not set in .env"
    exit 1
fi

if [ -z "${ETHEREUM_MAINNET_RPC:-}" ]; then
    log_error "ETHEREUM_MAINNET_RPC not set in .env"
    exit 1
fi

# Contract definitions
declare -A CONTRACTS=(
    ["0x89dd12025bfCD38A168455A44B400e913ED33BE2"]="CCIPWETH9Bridge|contracts/ccip/CCIPWETH9Bridge.sol|0x80226fc0Ee2b096224EeAc085Bb9a8cba1146f7D|0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2|0x514910771AF9Ca656af840dff83E8264EcF986CA"
)

# Results tracking
VERIFIED_COUNT=0
PENDING_COUNT=0
FAILED_COUNT=0
ALREADY_VERIFIED_COUNT=0

# Function to check if contract is already verified
check_verification_status() {
    local address=$1
    local api_key=$2
    
    log_info "Checking verification status for $address..."
    
    # Use Etherscan API to check verification status
    local response=$(curl -s "https://api.etherscan.io/api?module=contract&action=getsourcecode&address=${address}&apikey=${api_key}" 2>/dev/null || echo "")
    
    if [ -z "$response" ]; then
        log_warn "Could not check verification status via API"
        return 1
    fi
    
    # Check if contract is verified (SourceCode field is not empty)
    local sourcecode=$(echo "$response" | grep -o '"SourceCode":"[^"]*"' | head -1 | cut -d'"' -f4 || echo "")
    
    if [ -n "$sourcecode" ] && [ "$sourcecode" != "" ] && [ "$sourcecode" != "{{" ]; then
        return 0  # Verified
    else
        return 1  # Not verified
    fi
}

# Function to verify contract using Standard JSON Input
verify_with_standard_json() {
    local address=$1
    local contract_name=$2
    local standard_json_file=$3
    local constructor_args=$4
    
    log_step "Attempting verification with Standard JSON Input..."
    
    if [ ! -f "$standard_json_file" ]; then
        log_error "Standard JSON file not found: $standard_json_file"
        return 1
    fi
    
    # Use Etherscan API to submit Standard JSON verification
    local api_url="https://api.etherscan.io/api"
    
    # Read the Standard JSON file
    local standard_json_content=$(cat "$standard_json_file")
    
    # Prepare the API request
    # Note: Etherscan API requires multipart form data, so we'll use curl with form data
    log_info "Submitting verification request to Etherscan..."
    
    # Use forge verify-contract with Standard JSON if supported, otherwise manual API call
    # For now, we'll use the manual API approach
    local response=$(curl -s -X POST "$api_url" \
        -F "module=contract" \
        -F "action=verifysourcecode" \
        -F "apikey=${ETHERSCAN_API_KEY}" \
        -F "contractaddress=${address}" \
        -F "codeformat=solidity-standard-json-input" \
        -F "contractname=${contract_name}" \
        -F "compilerversion=v0.8.20+commit.a1b79de6" \
        -F "optimizationUsed=1" \
        -F "runs=200" \
        -F "constructorArguements=${constructor_args}" \
        -F "sourceCode=@${standard_json_file}" 2>/dev/null || echo "")
    
    if echo "$response" | grep -q '"status":"1"'; then
        local guid=$(echo "$response" | grep -o '"result":"[^"]*"' | cut -d'"' -f4 || echo "")
        if [ -n "$guid" ]; then
            log_success "Verification submitted! GUID: $guid"
            log_info "Check status: https://etherscan.io/address/${address}#code"
            return 0
        fi
    else
        local error=$(echo "$response" | grep -o '"result":"[^"]*"' | cut -d'"' -f4 || echo "Unknown error")
        log_warn "Verification submission failed: $error"
        return 1
    fi
}

# Function to verify contract using forge (fallback method)
verify_with_forge() {
    local address=$1
    local contract_path=$2
    local contract_name=$3
    local constructor_args=$4
    
    log_step "Attempting verification with Forge (without via-ir)..."
    
    cd "$SOURCE_PROJECT"
    
    if forge verify-contract \
        "$address" \
        "${contract_path}:${contract_name}" \
        --chain-id 1 \
        --etherscan-api-key "$ETHERSCAN_API_KEY" \
        --constructor-args "$constructor_args" \
        --compiler-version "0.8.20" \
        --num-of-optimizations 200 \
        --watch 2>&1 | tee /tmp/forge-verify.log; then
        
        log_success "Contract verified successfully with Forge!"
        return 0
    else
        log_warn "Forge verification failed (check /tmp/forge-verify.log)"
        return 1
    fi
}

# Function to verify a single contract
verify_contract() {
    local address=$1
    local contract_info=$2
    
    IFS='|' read -r contract_name contract_path router weth9 fee_token <<< "$contract_info"
    
    log_info ""
    log_info "========================================="
    log_info "Verifying: $contract_name"
    log_info "Address: $address"
    log_info "========================================="
    
    # Step 1: Check if contract exists on Mainnet
    log_step "Step 1: Checking if contract exists on Ethereum Mainnet..."
    local code=$(cast code "$address" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
    if [ -z "$code" ] || [ "$code" = "0x" ]; then
        log_error "Contract not found on Ethereum Mainnet"
        FAILED_COUNT=$((FAILED_COUNT + 1))
        return 1
    fi
    log_success "Contract exists on Ethereum Mainnet"
    
    # Step 2: Check if already verified
    log_step "Step 2: Checking if contract is already verified..."
    if check_verification_status "$address" "$ETHERSCAN_API_KEY"; then
        log_success "Contract is already verified on Etherscan!"
        ALREADY_VERIFIED_COUNT=$((ALREADY_VERIFIED_COUNT + 1))
        return 0
    fi
    log_info "Contract is not yet verified"
    
    # Step 3: Prepare constructor arguments
    log_step "Step 3: Preparing constructor arguments..."
    local constructor_args=$(cast abi-encode "constructor(address,address,address)" "$router" "$weth9" "$fee_token")
    log_info "Constructor args: $constructor_args"
    
    # Step 4: Try verification with Standard JSON (preferred method)
    log_step "Step 4: Attempting verification with Standard JSON Input..."
    local standard_json_file="$DOCS_DIR/CCIPWETH9Bridge_standard_json.json"
    
    if verify_with_standard_json "$address" "$contract_name" "$standard_json_file" "$constructor_args"; then
        log_success "Verification submitted successfully!"
        VERIFIED_COUNT=$((VERIFIED_COUNT + 1))
        
        # Wait a bit and check status
        log_info "Waiting 10 seconds for verification to process..."
        sleep 10
        
        if check_verification_status "$address" "$ETHERSCAN_API_KEY"; then
            log_success "Contract is now verified!"
            return 0
        else
            log_info "Verification submitted but not yet confirmed. Check Etherscan manually."
            PENDING_COUNT=$((PENDING_COUNT + 1))
            return 0
        fi
    fi
    
    # Step 5: Fallback to Forge verification (without via-ir)
    log_step "Step 5: Trying fallback verification method..."
    if verify_with_forge "$address" "$contract_path" "$contract_name" "$constructor_args"; then
        log_success "Contract verified with fallback method!"
        VERIFIED_COUNT=$((VERIFIED_COUNT + 1))
        return 0
    fi
    
    # Step 6: Manual verification instructions
    log_warn "Automated verification failed. Manual verification required."
    log_info ""
    log_info "Manual verification steps:"
    log_info "1. Go to: https://etherscan.io/address/${address}#code"
    log_info "2. Click 'Contract' tab → 'Verify and Publish'"
    log_info "3. Select 'Standard JSON Input'"
    log_info "4. Upload: $standard_json_file"
    log_info "5. Enter constructor args: $constructor_args"
    log_info "6. Submit"
    log_info ""
    
    FAILED_COUNT=$((FAILED_COUNT + 1))
    return 1
}

# Main execution
main() {
    log_info "========================================="
    log_info "Ethereum Mainnet Contract Verification"
    log_info "========================================="
    log_info ""
    
    # Check prerequisites
    log_step "Checking prerequisites..."
    
    if ! command -v cast &> /dev/null; then
        log_error "cast command not found. Please install Foundry."
        exit 1
    fi
    
    if ! command -v forge &> /dev/null; then
        log_error "forge command not found. Please install Foundry."
        exit 1
    fi
    
    if ! command -v curl &> /dev/null; then
        log_error "curl command not found. Please install curl."
        exit 1
    fi
    
    log_success "All prerequisites met"
    log_info ""
    
    # Verify each contract
    for address in "${!CONTRACTS[@]}"; do
        verify_contract "$address" "${CONTRACTS[$address]}"
    done
    
    # Summary
    log_info ""
    log_info "========================================="
    log_info "Verification Summary"
    log_info "========================================="
    log_info ""
    log_success "Already Verified: $ALREADY_VERIFIED_COUNT"
    log_success "Newly Verified: $VERIFIED_COUNT"
    log_warn "Pending/Submitted: $PENDING_COUNT"
    log_error "Failed: $FAILED_COUNT"
    log_info ""
    
    local total=$((ALREADY_VERIFIED_COUNT + VERIFIED_COUNT + PENDING_COUNT + FAILED_COUNT))
    log_info "Total Contracts: $total"
    
    # Update documentation
    log_info ""
    log_step "Updating documentation..."
    update_documentation
    
    log_info ""
    if [ $FAILED_COUNT -eq 0 ] && [ $PENDING_COUNT -eq 0 ]; then
        log_success "All contracts verified successfully!"
    elif [ $FAILED_COUNT -eq 0 ]; then
        log_success "All verification requests submitted successfully!"
        log_info "Check Etherscan in a few minutes to confirm verification."
    else
        log_warn "Some contracts require manual verification."
        log_info "See instructions above for manual verification steps."
    fi
}

# Function to update documentation
update_documentation() {
    local status_file="$DOCS_DIR/ETHEREUM_MAINNET_CONTRACTS_VERIFICATION_STATUS.md"
    
    if [ ! -f "$status_file" ]; then
        log_warn "Status file not found: $status_file"
        return
    fi
    
    local verified_total=$((ALREADY_VERIFIED_COUNT + VERIFIED_COUNT))
    local pending_total=$PENDING_COUNT
    
    # Update summary table
    sed -i "s/| \*\*1\*\* | \*\*0\*\* | \*\*1\*\* |/| **$((verified_total + pending_total + FAILED_COUNT))** | **$verified_total** | **$pending_total** |/" "$status_file" 2>/dev/null || true
    
    log_success "Documentation updated"
}

# Run main function
main "$@"

