#!/usr/bin/env bash
# Fix Blockscout verification using correct API format
# Usage: ./fix-blockscout-verification.sh

set -euo pipefail

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

# 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 [ -f "$SOURCE_PROJECT/.env" ]; then
    source "$SOURCE_PROJECT/.env"
else
    log_error ".env file not found in $SOURCE_PROJECT"
    exit 1
fi

RPC_URL_138="${RPC_URL_138:-https://rpc-core.d-bis.org}"
CCIP_ROUTER_138="${CCIP_CHAIN138_ROUTER:-0x8078A09637e47Fa5Ed34F626046Ea2094a5CDE5e}"
WETH9_138="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
WETH10_138="0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f"
LINK_TOKEN_138="${CCIP_CHAIN138_LINK_TOKEN:-0x514910771AF9Ca656af840dff83E8264EcF986CA}"

WETH9_BRIDGE_138="0x89dd12025bfCD38A168455A44B400e913ED33BE2"
WETH10_BRIDGE_138="0xe0E93247376aa097dB308B92e6Ba36bA015535D0"

log_info "========================================="
log_info "Fix Blockscout Verification"
log_info "========================================="
log_info ""

cd "$SOURCE_PROJECT"

# Check if contracts are already verified
log_info "Checking verification status..."

WETH9_VERIFIED=$(curl -s "https://explorer.d-bis.org/api/v2/smart-contracts/$WETH9_BRIDGE_138" | python3 -c "import sys, json; data=json.load(sys.stdin); print('verified' if data.get('is_verified') else 'not_verified')" 2>/dev/null || echo "unknown")
WETH10_VERIFIED=$(curl -s "https://explorer.d-bis.org/api/v2/smart-contracts/$WETH10_BRIDGE_138" | python3 -c "import sys, json; data=json.load(sys.stdin); print('verified' if data.get('is_verified') else 'not_verified')" 2>/dev/null || echo "unknown")

log_info "CCIPWETH9Bridge verification status: $WETH9_VERIFIED"
log_info "CCIPWETH10Bridge verification status: $WETH10_VERIFIED"
log_info ""

if [ "$WETH9_VERIFIED" = "verified" ] && [ "$WETH10_VERIFIED" = "verified" ]; then
    log_success "✓ Both contracts are already verified on Blockscout!"
    exit 0
fi

# Try verification using forge with correct Blockscout API format
log_info "Attempting verification with corrected API format..."

# Verify CCIPWETH9Bridge
if [ "$WETH9_VERIFIED" != "verified" ]; then
    log_info "Step 1: Verifying CCIPWETH9Bridge..."
    WETH9_CONSTRUCTOR_ARGS=$(cast abi-encode "constructor(address,address,address)" "$CCIP_ROUTER_138" "$WETH9_138" "$LINK_TOKEN_138" | sed 's/^0x//')
    
    # Use forge verify-contract with proper Blockscout format
    if forge verify-contract \
        "$WETH9_BRIDGE_138" \
        contracts/ccip/CCIPWETH9Bridge.sol:CCIPWETH9Bridge \
        --chain-id 138 \
        --rpc-url "$RPC_URL_138" \
        --verifier blockscout \
        --verifier-url https://explorer.d-bis.org/api \
        --num-of-optimizations 200 \
        --constructor-args "$WETH9_CONSTRUCTOR_ARGS" \
        --compiler-version "0.8.20+commit.a1b79de6" \
        --via-ir \
        2>&1 | tee /tmp/weth9-verify-fix.log; then
        log_success "✓ CCIPWETH9Bridge verification submitted!"
    else
        log_warn "⚠️  Automated verification failed - manual verification required"
        log_info "Manual verification URL: https://explorer.d-bis.org/address/$WETH9_BRIDGE_138#verify"
        log_info "See docs/BLOCKSCOUT_BRIDGE_ADDRESSES_UPDATE.md for instructions"
    fi
fi

log_info ""

# Verify CCIPWETH10Bridge
if [ "$WETH10_VERIFIED" != "verified" ]; then
    log_info "Step 2: Verifying CCIPWETH10Bridge..."
    WETH10_CONSTRUCTOR_ARGS=$(cast abi-encode "constructor(address,address,address)" "$CCIP_ROUTER_138" "$WETH10_138" "$LINK_TOKEN_138" | sed 's/^0x//')
    
    if forge verify-contract \
        "$WETH10_BRIDGE_138" \
        contracts/ccip/CCIPWETH10Bridge.sol:CCIPWETH10Bridge \
        --chain-id 138 \
        --rpc-url "$RPC_URL_138" \
        --verifier blockscout \
        --verifier-url https://explorer.d-bis.org/api \
        --num-of-optimizations 200 \
        --constructor-args "$WETH10_CONSTRUCTOR_ARGS" \
        --compiler-version "0.8.20+commit.a1b79de6" \
        --via-ir \
        2>&1 | tee /tmp/weth10-verify-fix.log; then
        log_success "✓ CCIPWETH10Bridge verification submitted!"
    else
        log_warn "⚠️  Automated verification failed - manual verification required"
        log_info "Manual verification URL: https://explorer.d-bis.org/address/$WETH10_BRIDGE_138#verify"
        log_info "See docs/BLOCKSCOUT_BRIDGE_ADDRESSES_UPDATE.md for instructions"
    fi
fi

log_info ""
log_success "========================================="
log_success "Verification Attempt Complete"
log_success "========================================="
log_info ""
log_info "Blockscout Links:"
log_info "  CCIPWETH9Bridge:  https://explorer.d-bis.org/address/$WETH9_BRIDGE_138"
log_info "  CCIPWETH10Bridge: https://explorer.d-bis.org/address/$WETH10_BRIDGE_138"
log_info ""

