#!/usr/bin/env bash
# Check for existing contract deployments on Ethereum Mainnet and Chain-138

set -e

cd "$(dirname "$0")/../.."

# Color codes

echo "==================================================================="
echo "  EXISTING DEPLOYMENT CHECK"
echo "==================================================================="

# Load environment variables
if [ -f .env ]; then
    source .env 2>/dev/null || true
fi

# Check if required tools are available
if ! command -v cast &> /dev/null; then
    log_warn "⚠️  cast (Foundry) not found. Some checks may be limited."
    CAST_AVAILABLE=false
else
    CAST_AVAILABLE=true
fi

log_info "📋 Checking Ethereum Mainnet Deployments"

# WETH9 and WETH10 canonical addresses
WETH9_ADDRESS="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
WETH10_ADDRESS="0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f"

log_info "1. Predeployed Contracts (Canonical Addresses)"

# Check WETH9
if [ "$CAST_AVAILABLE" = true ] && [ -n "$ETHEREUM_MAINNET_RPC" ]; then
    WETH9_CODE=$(cast code "$WETH9_ADDRESS" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
    if [ -n "$WETH9_CODE" ] && [ "$WETH9_CODE" != "0x" ]; then
        echo -e "  ${GREEN}✅ WETH9${NC}"
        echo "    Address: $WETH9_ADDRESS"
        echo "    Status: Deployed (verified on-chain)"
    else
        echo -e "  ${RED}❌ WETH9${NC}"
        echo "    Address: $WETH9_ADDRESS"
        echo "    Status: Not found or no code"
    fi
else
    echo -e "  ${YELLOW}⚠️  WETH9${NC}"
    echo "    Address: $WETH9_ADDRESS"
    echo "    Status: Cannot verify (RPC not configured or cast not available)"
fi

# Check WETH10
if [ "$CAST_AVAILABLE" = true ] && [ -n "$ETHEREUM_MAINNET_RPC" ]; then
    WETH10_CODE=$(cast code "$WETH10_ADDRESS" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
    if [ -n "$WETH10_CODE" ] && [ "$WETH10_CODE" != "0x" ]; then
        echo -e "  ${GREEN}✅ WETH10${NC}"
        echo "    Address: $WETH10_ADDRESS"
        echo "    Status: Deployed (verified on-chain)"
    else
        echo -e "  ${RED}❌ WETH10${NC}"
        echo "    Address: $WETH10_ADDRESS"
        echo "    Status: Not found or no code"
    fi
else
    echo -e "  ${YELLOW}⚠️  WETH10${NC}"
    echo "    Address: $WETH10_ADDRESS"
    echo "    Status: Cannot verify (RPC not configured or cast not available)"
fi

log_info "2. CCIP Integration Contracts"

# Check CCIPLogger
if [ -n "$MAINNET_CCIP_LOGGER" ] && [ "$MAINNET_CCIP_LOGGER" != "" ]; then
    if [ "$CAST_AVAILABLE" = true ] && [ -n "$ETHEREUM_MAINNET_RPC" ]; then
        LOGGER_CODE=$(cast code "$MAINNET_CCIP_LOGGER" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
        if [ -n "$LOGGER_CODE" ] && [ "$LOGGER_CODE" != "0x" ]; then
            echo -e "  ${GREEN}✅ CCIPLogger${NC}"
            echo "    Address: $MAINNET_CCIP_LOGGER"
            echo "    Status: Deployed (verified on-chain)"
        else
            echo -e "  ${RED}❌ CCIPLogger${NC}"
            echo "    Address: $MAINNET_CCIP_LOGGER"
            echo "    Status: Address in .env but no code found"
        fi
    else
        echo -e "  ${YELLOW}⚠️  CCIPLogger${NC}"
        echo "    Address: $MAINNET_CCIP_LOGGER (from .env)"
        echo "    Status: Cannot verify (RPC not configured)"
    fi
else
    echo -e "  ${RED}❌ CCIPLogger${NC}"
    echo "    Status: Not deployed (no address in .env)"
fi

log_info "3. WETH Bridge Contracts"

# Check CCIPWETH9Bridge
if [ -n "$MAINNET_CCIP_WETH9_BRIDGE" ] && [ "$MAINNET_CCIP_WETH9_BRIDGE" != "" ]; then
    if [ "$CAST_AVAILABLE" = true ] && [ -n "$ETHEREUM_MAINNET_RPC" ]; then
        BRIDGE9_CODE=$(cast code "$MAINNET_CCIP_WETH9_BRIDGE" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
        if [ -n "$BRIDGE9_CODE" ] && [ "$BRIDGE9_CODE" != "0x" ]; then
            echo -e "  ${GREEN}✅ CCIPWETH9Bridge${NC}"
            echo "    Address: $MAINNET_CCIP_WETH9_BRIDGE"
            echo "    Status: Deployed (verified on-chain)"
        else
            echo -e "  ${RED}❌ CCIPWETH9Bridge${NC}"
            echo "    Address: $MAINNET_CCIP_WETH9_BRIDGE"
            echo "    Status: Address in .env but no code found"
        fi
    else
        echo -e "  ${YELLOW}⚠️  CCIPWETH9Bridge${NC}"
        echo "    Address: $MAINNET_CCIP_WETH9_BRIDGE (from .env)"
        echo "    Status: Cannot verify (RPC not configured)"
    fi
else
    echo -e "  ${RED}❌ CCIPWETH9Bridge${NC}"
    echo "    Status: Not deployed (no address in .env)"
fi

# Check CCIPWETH10Bridge
if [ -n "$MAINNET_CCIP_WETH10_BRIDGE" ] && [ "$MAINNET_CCIP_WETH10_BRIDGE" != "" ]; then
    if [ "$CAST_AVAILABLE" = true ] && [ -n "$ETHEREUM_MAINNET_RPC" ]; then
        BRIDGE10_CODE=$(cast code "$MAINNET_CCIP_WETH10_BRIDGE" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
        if [ -n "$BRIDGE10_CODE" ] && [ "$BRIDGE10_CODE" != "0x" ]; then
            echo -e "  ${GREEN}✅ CCIPWETH10Bridge${NC}"
            echo "    Address: $MAINNET_CCIP_WETH10_BRIDGE"
            echo "    Status: Deployed (verified on-chain)"
        else
            echo -e "  ${RED}❌ CCIPWETH10Bridge${NC}"
            echo "    Address: $MAINNET_CCIP_WETH10_BRIDGE"
            echo "    Status: Address in .env but no code found"
        fi
    else
        echo -e "  ${YELLOW}⚠️  CCIPWETH10Bridge${NC}"
        echo "    Address: $MAINNET_CCIP_WETH10_BRIDGE (from .env)"
        echo "    Status: Cannot verify (RPC not configured)"
    fi
else
    echo -e "  ${RED}❌ CCIPWETH10Bridge${NC}"
    echo "    Status: Not deployed (no address in .env)"
fi

log_info "4. Chain-138 Deployments"

# Check CCIPTxReporter on Chain-138
if [ -n "$CHAIN138_CCIP_REPORTER" ] && [ "$CHAIN138_CCIP_REPORTER" != "" ]; then
    if [ "$CAST_AVAILABLE" = true ] && [ -n "$CHAIN138_RPC_URL" ]; then
        REPORTER_CODE=$(cast code "$CHAIN138_CCIP_REPORTER" --rpc-url "$CHAIN138_RPC_URL" 2>/dev/null || echo "")
        if [ -n "$REPORTER_CODE" ] && [ "$REPORTER_CODE" != "0x" ]; then
            echo -e "  ${GREEN}✅ CCIPTxReporter (Chain-138)${NC}"
            echo "    Address: $CHAIN138_CCIP_REPORTER"
            echo "    Status: Deployed (verified on-chain)"
        else
            echo -e "  ${YELLOW}⚠️  CCIPTxReporter (Chain-138)${NC}"
            echo "    Address: $CHAIN138_CCIP_REPORTER"
            echo "    Status: Address in .env but cannot verify (RPC may not be accessible)"
        fi
    else
        echo -e "  ${YELLOW}⚠️  CCIPTxReporter (Chain-138)${NC}"
        echo "    Address: $CHAIN138_CCIP_REPORTER (from .env)"
        echo "    Status: Cannot verify (RPC not configured or not accessible)"
    fi
else
    echo -e "  ${RED}❌ CCIPTxReporter (Chain-138)${NC}"
    echo "    Status: Not deployed (no address in .env)"
fi

log_info "5. Summary"

DEPLOYED_COUNT=0
TOTAL_COUNT=5

# Count deployed contracts
if [ -n "$MAINNET_CCIP_LOGGER" ] && [ "$MAINNET_CCIP_LOGGER" != "" ]; then
    DEPLOYED_COUNT=$((DEPLOYED_COUNT + 1))
fi
if [ -n "$MAINNET_CCIP_WETH9_BRIDGE" ] && [ "$MAINNET_CCIP_WETH9_BRIDGE" != "" ]; then
    DEPLOYED_COUNT=$((DEPLOYED_COUNT + 1))
fi
if [ -n "$MAINNET_CCIP_WETH10_BRIDGE" ] && [ "$MAINNET_CCIP_WETH10_BRIDGE" != "" ]; then
    DEPLOYED_COUNT=$((DEPLOYED_COUNT + 1))
fi
if [ -n "$CHAIN138_CCIP_REPORTER" ] && [ "$CHAIN138_CCIP_REPORTER" != "" ]; then
    DEPLOYED_COUNT=$((DEPLOYED_COUNT + 1))
fi

echo "  Contracts with addresses in .env: $DEPLOYED_COUNT/$TOTAL_COUNT"
echo "  WETH9/WETH10: Predeployed at canonical addresses"

if [ $DEPLOYED_COUNT -eq 0 ]; then
    log_error "❌ No contracts deployed yet"
    echo "  All contracts need to be deployed"
elif [ $DEPLOYED_COUNT -lt $TOTAL_COUNT ]; then
    log_warn "⚠️  Partial deployment"
    echo "  Some contracts still need deployment"
else
    log_success "✅ All contracts have addresses configured"
    echo "  Verify on-chain status above"
fi

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