#!/usr/bin/env bash
# Verify Ethereum Mainnet contracts on Etherscan
# This script checks verification status and provides verification commands

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$PROJECT_ROOT"
# 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


echo "=========================================="
echo "Ethereum Mainnet Contract Verification"
echo "=========================================="
echo ""

# Mainnet contract addresses (prefer env-backed canonical values)
CCIPWETH9BRIDGE_MAINNET="${MAINNET_CCIP_WETH9_BRIDGE:-0xc9901ce2Ddb6490FAA183645147a87496d8b20B6}"
CCIPWETH10BRIDGE_MAINNET="${MAINNET_CCIP_WETH10_BRIDGE:-0x04E1e22B0D41e99f4275bd40A50480219bc9A223}"

# Check verification status
check_verification() {
    local contract_name=$1
    local contract_address=$2

    echo "Checking $contract_name ($contract_address)..."

    if [ -z "${ETHERSCAN_API_KEY:-}" ]; then
        echo "  ⚠️  ETHERSCAN_API_KEY not set - cannot check via API"
        echo "  📄 Check manually: https://etherscan.io/address/$contract_address#code"
        return
    fi

    local response=$(curl -s "https://api.etherscan.io/api?module=contract&action=getsourcecode&address=$contract_address&apikey=$ETHERSCAN_API_KEY")
    local sourcecode=$(echo "$response" | jq -r '.result[0].SourceCode' 2>/dev/null || echo "")
    local contract_name_api=$(echo "$response" | jq -r '.result[0].ContractName' 2>/dev/null || echo "")

    if [ -n "$sourcecode" ] && [ "$sourcecode" != "" ]; then
        echo "  ✅ Contract is VERIFIED"
        if [ -n "$contract_name_api" ] && [ "$contract_name_api" != "" ]; then
            echo "  Contract Name: $contract_name_api"
        fi
        echo "  📄 View: https://etherscan.io/address/$contract_address#code"
    else
        echo "  ❌ Contract is NOT VERIFIED"
        echo "  📄 Verify at: https://etherscan.io/address/$contract_address#code"
        echo ""
        # Get contract name for verification
        local contract_file=""
        local contract_name_short=""
        if [[ "$contract_name" == *"9Bridge"* ]]; then
            contract_file="contracts/ccip/CCIPWETH9Bridge.sol:CCIPWETH9Bridge"
            contract_name_short="CCIPWETH9Bridge"
        elif [[ "$contract_name" == *"10Bridge"* ]]; then
            contract_file="contracts/ccip/CCIPWETH10Bridge.sol:CCIPWETH10Bridge"
            contract_name_short="CCIPWETH10Bridge"
        else
            contract_file="contracts/ccip/${contract_name}.sol:${contract_name}"
            contract_name_short="$contract_name"
        fi

        echo "  Verification Command (Foundry):"
        echo "  forge verify-contract \\"
        echo "    --chain-id 1 \\"
        echo "    --num-of-optimizations 200 \\"
        echo "    --watch \\"
        echo "    $contract_address \\"
        echo "    $contract_file \\"
        if [ -n "${ETHERSCAN_API_KEY:-}" ]; then
            echo "    $ETHERSCAN_API_KEY"
        else
            echo "    <ETHERSCAN_API_KEY>"
        fi
        echo ""
        echo "  Or verify via Etherscan UI:"
        echo "  https://etherscan.io/address/$contract_address#code"
    fi
    echo ""
}

# Check both contracts
check_verification "CCIPWETH9Bridge" "$CCIPWETH9BRIDGE_MAINNET"
check_verification "CCIPWETH10Bridge" "$CCIPWETH10BRIDGE_MAINNET"

echo "=========================================="
echo "Verification Check Complete"
echo "=========================================="
echo ""
echo "📋 Quick Links:"
echo "  • CCIPWETH9Bridge: https://etherscan.io/address/$CCIPWETH9BRIDGE_MAINNET"
echo "  • CCIPWETH10Bridge: https://etherscan.io/address/$CCIPWETH10BRIDGE_MAINNET"
echo "  • Verify Contracts: https://etherscan.io/myverify_address"
echo ""
