#!/usr/bin/env bash
# Verify Ethereum Mainnet contracts on Etherscan using Foundry
# This script provides the exact commands needed to verify contracts

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
CCIPWETH9BRIDGE_MAINNET="${MAINNET_CCIP_WETH9_BRIDGE:-0xc9901ce2Ddb6490FAA183645147a87496d8b20B6}"
CCIPWETH10BRIDGE_MAINNET="${MAINNET_CCIP_WETH10_BRIDGE:-0x04E1e22B0D41e99f4275bd40A50480219bc9A223}"

# Constructor arguments
CCIP_ROUTER="${CCIP_MAINNET_ROUTER:-0x80226fc0Ee2b096224EeAc085Bb9a8cba1146f7D}"
WETH9_MAINNET="${WETH9_MAINNET:-0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2}"
WETH10_MAINNET="${WETH10_MAINNET:-0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f}"
LINK_TOKEN="${CCIP_MAINNET_LINK:-0x514910771AF9Ca656af840dff83E8264EcF986CA}"

if [ -z "${ETHERSCAN_API_KEY:-}" ]; then
    echo "⚠️  ETHERSCAN_API_KEY not set in .env"
    echo "   Please add your Etherscan API key to .env"
    echo ""
    echo "   Get API key from: https://etherscan.io/myapikey"
    exit 1
fi

echo "Verifying CCIPWETH9Bridge..."
echo "Address: $CCIPWETH9BRIDGE_MAINNET"
echo "Constructor Args:"
echo "  Router: $CCIP_ROUTER"
echo "  WETH9: $WETH9_MAINNET"
echo "  LINK: $LINK_TOKEN"
echo ""

# Encode constructor arguments
CONSTRUCTOR_ARGS=$(cast abi-encode "constructor(address,address,address)" "$CCIP_ROUTER" "$WETH9_MAINNET" "$LINK_TOKEN")

echo "Running verification..."
forge verify-contract \
    --chain-id 1 \
    --num-of-optimizations 200 \
    --watch \
    --constructor-args "$CONSTRUCTOR_ARGS" \
    --etherscan-api-key "$ETHERSCAN_API_KEY" \
    "$CCIPWETH9BRIDGE_MAINNET" \
    "contracts/ccip/CCIPWETH9Bridge.sol:CCIPWETH9Bridge"

echo ""
echo "=========================================="
echo ""

echo "Verifying CCIPWETH10Bridge..."
echo "Address: $CCIPWETH10BRIDGE_MAINNET"
echo "Constructor Args:"
echo "  Router: $CCIP_ROUTER"
echo "  WETH10: $WETH10_MAINNET"
echo "  LINK: $LINK_TOKEN"
echo ""

# Encode constructor arguments
CONSTRUCTOR_ARGS=$(cast abi-encode "constructor(address,address,address)" "$CCIP_ROUTER" "$WETH10_MAINNET" "$LINK_TOKEN")

echo "Running verification..."
forge verify-contract \
    --chain-id 1 \
    --num-of-optimizations 200 \
    --watch \
    --constructor-args "$CONSTRUCTOR_ARGS" \
    --etherscan-api-key "$ETHERSCAN_API_KEY" \
    "$CCIPWETH10BRIDGE_MAINNET" \
    "contracts/ccip/CCIPWETH10Bridge.sol:CCIPWETH10Bridge"

echo ""
echo "=========================================="
echo "Verification Complete"
echo "=========================================="
echo ""
echo "📄 View contracts:"
echo "  • CCIPWETH9Bridge: https://etherscan.io/address/$CCIPWETH9BRIDGE_MAINNET"
echo "  • CCIPWETH10Bridge: https://etherscan.io/address/$CCIPWETH10BRIDGE_MAINNET"
echo ""
