#!/usr/bin/env bash
# Generate comprehensive deployment status report

set -e

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

# Color codes

echo "==================================================================="
echo "  DEPLOYMENT STATUS REPORT"
echo "==================================================================="

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

# Check for cast
if ! command -v cast &> /dev/null; then
    CAST_AVAILABLE=false
else
    CAST_AVAILABLE=true
fi

log_info "📋 Ethereum Mainnet Contracts"

# WETH9
WETH9_ADDR="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
if [ "$CAST_AVAILABLE" = true ] && [ -n "$ETHEREUM_MAINNET_RPC" ]; then
    WETH9_CODE=$(cast code "$WETH9_ADDR" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
    if [ -n "$WETH9_CODE" ] && [ "$WETH9_CODE" != "0x" ] && [ ${#WETH9_CODE} -gt 2 ]; then
        echo -e "  ${GREEN}✅ WETH9${NC} - $WETH9_ADDR (Canonical Mainnet)"
    else
        echo -e "  ${RED}❌ WETH9${NC} - $WETH9_ADDR (Not found on-chain)"
    fi
else
    echo -e "  ${YELLOW}⚠️  WETH9${NC} - $WETH9_ADDR (Cannot verify)"
fi

# WETH10
WETH10_ADDR="0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f"
if [ "$CAST_AVAILABLE" = true ] && [ -n "$ETHEREUM_MAINNET_RPC" ]; then
    WETH10_CODE=$(cast code "$WETH10_ADDR" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
    if [ -n "$WETH10_CODE" ] && [ "$WETH10_CODE" != "0x" ] && [ ${#WETH10_CODE} -gt 2 ]; then
        echo -e "  ${GREEN}✅ WETH10${NC} - $WETH10_ADDR (Canonical Mainnet)"
    else
        echo -e "  ${RED}❌ WETH10${NC} - $WETH10_ADDR (Not found on-chain)"
    fi
else
    echo -e "  ${YELLOW}⚠️  WETH10${NC} - $WETH10_ADDR (Cannot verify)"
fi

# 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" ] && [ ${#LOGGER_CODE} -gt 2 ]; then
            echo -e "  ${GREEN}✅ CCIPLogger${NC} - $MAINNET_CCIP_LOGGER"
        else
            echo -e "  ${RED}❌ CCIPLogger${NC} - $MAINNET_CCIP_LOGGER (Address in .env but not on-chain)"
        fi
    else
        echo -e "  ${YELLOW}⚠️  CCIPLogger${NC} - $MAINNET_CCIP_LOGGER (Address in .env, cannot verify)"
    fi
else
    echo -e "  ${RED}❌ CCIPLogger${NC} - Not deployed (no address in .env)"
fi

# 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" ] && [ ${#BRIDGE9_CODE} -gt 2 ]; then
            echo -e "  ${GREEN}✅ CCIPWETH9Bridge${NC} - $MAINNET_CCIP_WETH9_BRIDGE"
        else
            echo -e "  ${RED}❌ CCIPWETH9Bridge${NC} - $MAINNET_CCIP_WETH9_BRIDGE (Address in .env but not on-chain)"
        fi
    else
        echo -e "  ${YELLOW}⚠️  CCIPWETH9Bridge${NC} - $MAINNET_CCIP_WETH9_BRIDGE (Address in .env, cannot verify)"
    fi
else
    echo -e "  ${RED}❌ CCIPWETH9Bridge${NC} - Not deployed (no address in .env)"
fi

# 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" ] && [ ${#BRIDGE10_CODE} -gt 2 ]; then
            echo -e "  ${GREEN}✅ CCIPWETH10Bridge${NC} - $MAINNET_CCIP_WETH10_BRIDGE"
        else
            echo -e "  ${RED}❌ CCIPWETH10Bridge${NC} - $MAINNET_CCIP_WETH10_BRIDGE (Address in .env but not on-chain)"
        fi
    else
        echo -e "  ${YELLOW}⚠️  CCIPWETH10Bridge${NC} - $MAINNET_CCIP_WETH10_BRIDGE (Address in .env, cannot verify)"
    fi
else
    echo -e "  ${RED}❌ CCIPWETH10Bridge${NC} - Not deployed (no address in .env)"
fi

log_info "📋 Chain-138 Contracts"

# CCIPTxReporter
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" ] && [ ${#REPORTER_CODE} -gt 2 ]; then
            echo -e "  ${GREEN}✅ CCIPTxReporter${NC} - $CHAIN138_CCIP_REPORTER"
        else
            echo -e "  ${YELLOW}⚠️  CCIPTxReporter${NC} - $CHAIN138_CCIP_REPORTER (Address in .env, RPC may not be accessible)"
        fi
    else
        echo -e "  ${YELLOW}⚠️  CCIPTxReporter${NC} - $CHAIN138_CCIP_REPORTER (Address in .env, cannot verify)"
    fi
else
    echo -e "  ${RED}❌ CCIPTxReporter${NC} - Not deployed (no address in .env)"
fi

log_info "📊 Summary"

# Count deployments
MAINNET_DEPLOYED=0
MAINNET_TOTAL=3

if [ -n "$MAINNET_CCIP_LOGGER" ] && [ "$MAINNET_CCIP_LOGGER" != "" ]; then
    MAINNET_DEPLOYED=$((MAINNET_DEPLOYED + 1))
fi
if [ -n "$MAINNET_CCIP_WETH9_BRIDGE" ] && [ "$MAINNET_CCIP_WETH9_BRIDGE" != "" ]; then
    MAINNET_DEPLOYED=$((MAINNET_DEPLOYED + 1))
fi
if [ -n "$MAINNET_CCIP_WETH10_BRIDGE" ] && [ "$MAINNET_CCIP_WETH10_BRIDGE" != "" ]; then
    MAINNET_DEPLOYED=$((MAINNET_DEPLOYED + 1))
fi

echo "  Mainnet Contracts: $MAINNET_DEPLOYED/$MAINNET_TOTAL deployed"
echo "  Chain-138 Contracts: $([ -n "$CHAIN138_CCIP_REPORTER" ] && echo "1/1" || echo "0/1") deployed"
echo "  WETH9/WETH10: Predeployed at canonical addresses"

if [ $MAINNET_DEPLOYED -eq 0 ]; then
    log_error "❌ No Mainnet contracts deployed yet"
elif [ $MAINNET_DEPLOYED -lt $MAINNET_TOTAL ]; then
    log_warn "⚠️  Partial Mainnet deployment"
    echo "  Remaining: $((MAINNET_TOTAL - MAINNET_DEPLOYED)) contracts"
else
    log_success "✅ All Mainnet contracts have addresses configured"
fi

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