#!/usr/bin/env bash
# Check all possible sources for deployment addresses

set -e

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

# Color codes

echo "=== Checking All Deployment Sources ==="

# 1. Check .env file
log_info "1. Environment Variables (.env)"
if [ -f .env ]; then
    echo "  ✅ .env file exists"
    
    # Check for Mainnet addresses
    MAINNET_VARS=$(grep -E "MAINNET_|ETHEREUM_" .env 2>/dev/null | grep -v "^#" | wc -l)
    CHAIN138_VARS=$(grep -E "CHAIN138_" .env 2>/dev/null | grep -v "^#" | wc -l)
    
    echo "  Mainnet variables: $MAINNET_VARS"
    echo "  Chain-138 variables: $CHAIN138_VARS"
    
    # List specific addresses if found
    if grep -q "MAINNET_CCIP_LOGGER" .env 2>/dev/null; then
        LOGGER=$(grep "MAINNET_CCIP_LOGGER" .env | cut -d'=' -f2 | tr -d '"' | tr -d "'" | xargs)
        if [ -n "$LOGGER" ] && [ "$LOGGER" != "" ]; then
            echo -e "  ${GREEN}✅ CCIPLogger: $LOGGER${NC}"
        fi
    fi
    
    if grep -q "MAINNET_CCIP_WETH9_BRIDGE" .env 2>/dev/null; then
        BRIDGE9=$(grep "MAINNET_CCIP_WETH9_BRIDGE" .env | cut -d'=' -f2 | tr -d '"' | tr -d "'" | xargs)
        if [ -n "$BRIDGE9" ] && [ "$BRIDGE9" != "" ]; then
            echo -e "  ${GREEN}✅ CCIPWETH9Bridge: $BRIDGE9${NC}"
        fi
    fi
    
    if grep -q "MAINNET_CCIP_WETH10_BRIDGE" .env 2>/dev/null; then
        BRIDGE10=$(grep "MAINNET_CCIP_WETH10_BRIDGE" .env | cut -d'=' -f2 | tr -d '"' | tr -d "'" | xargs)
        if [ -n "$BRIDGE10" ] && [ "$BRIDGE10" != "" ]; then
            echo -e "  ${GREEN}✅ CCIPWETH10Bridge: $BRIDGE10${NC}"
        fi
    fi
    
    if grep -q "CHAIN138_CCIP_REPORTER" .env 2>/dev/null; then
        REPORTER=$(grep "CHAIN138_CCIP_REPORTER" .env | cut -d'=' -f2 | tr -d '"' | tr -d "'" | xargs)
        if [ -n "$REPORTER" ] && [ "$REPORTER" != "" ]; then
            echo -e "  ${GREEN}✅ CCIPTxReporter: $REPORTER${NC}"
        fi
    fi
else
    echo -e "  ${RED}❌ .env file not found${NC}"
fi


# 2. Check Foundry broadcast files
log_info "2. Foundry Broadcast Files"
if [ -d "broadcast" ]; then
    echo "  ✅ broadcast directory exists"
    
    # Check for Mainnet deployments
    MAINNET_DEPLOYS=$(find broadcast -name "*.json" -type f 2>/dev/null | grep -E "(mainnet|1)" | wc -l)
    CHAIN138_DEPLOYS=$(find broadcast -name "*.json" -type f 2>/dev/null | grep -E "(138)" | wc -l)
    
    echo "  Mainnet deployment files: $MAINNET_DEPLOYS"
    echo "  Chain-138 deployment files: $CHAIN138_DEPLOYS"
    
    if [ $MAINNET_DEPLOYS -gt 0 ]; then
        echo "  Recent Mainnet deployments:"
        find broadcast -name "*.json" -type f 2>/dev/null | grep -E "(mainnet|1)" | head -5 | while read file; do
            echo "    - $file"
        done
    fi
else
    echo -e "  ${YELLOW}⚠️  broadcast directory not found${NC}"
fi


# 3. Check Hardhat artifacts
log_info "3. Hardhat Artifacts"
if [ -d "artifacts" ]; then
    echo "  ✅ artifacts directory exists"
    
    # Check for deployed contract addresses in artifacts
    DEPLOYED=$(find artifacts -name "*.json" -type f 2>/dev/null | xargs grep -l "deployedAddress\|address" 2>/dev/null | wc -l)
    echo "  Artifacts with addresses: $DEPLOYED"
else
    echo -e "  ${YELLOW}⚠️  artifacts directory not found${NC}"
fi


# 4. Check documentation files
log_info "4. Documentation Files"
DOCS_WITH_ADDRESSES=$(find docs -name "*.md" -type f 2>/dev/null | xargs grep -l "0x[a-fA-F0-9]\{40\}" 2>/dev/null | wc -l)
echo "  Documentation files with addresses: $DOCS_WITH_ADDRESSES"

if [ $DOCS_WITH_ADDRESSES -gt 0 ]; then
    echo "  Files containing addresses:"
    find docs -name "*.md" -type f 2>/dev/null | xargs grep -l "0x[a-fA-F0-9]\{40\}" 2>/dev/null | head -5 | while read file; do
        echo "    - $file"
    done
fi


# 5. Check for contract address files
log_info "5. Contract Address Files"
ADDRESS_FILES=$(find . -name "*address*.json" -o -name "*deploy*.json" -o -name "*contract*.json" 2>/dev/null | grep -v node_modules | grep -v ".git" | head -5)
if [ -n "$ADDRESS_FILES" ]; then
    echo "  Found address files:"
    echo "$ADDRESS_FILES" | while read file; do
        echo "    - $file"
    done
else
    echo -e "  ${YELLOW}⚠️  No contract address files found${NC}"
fi

echo "=== Summary ==="

# Count total found addresses
TOTAL_FOUND=0

if [ -f .env ]; then
    if grep -q "MAINNET_CCIP_LOGGER=" .env 2>/dev/null && [ -n "$(grep "MAINNET_CCIP_LOGGER" .env | cut -d'=' -f2 | tr -d ' ')" ]; then
        TOTAL_FOUND=$((TOTAL_FOUND + 1))
    fi
    if grep -q "MAINNET_CCIP_WETH9_BRIDGE=" .env 2>/dev/null && [ -n "$(grep "MAINNET_CCIP_WETH9_BRIDGE" .env | cut -d'=' -f2 | tr -d ' ')" ]; then
        TOTAL_FOUND=$((TOTAL_FOUND + 1))
    fi
    if grep -q "MAINNET_CCIP_WETH10_BRIDGE=" .env 2>/dev/null && [ -n "$(grep "MAINNET_CCIP_WETH10_BRIDGE" .env | cut -d'=' -f2 | tr -d ' ')" ]; then
        TOTAL_FOUND=$((TOTAL_FOUND + 1))
    fi
fi

echo "  Contracts with addresses found: $TOTAL_FOUND/3 (Mainnet)"
echo "  WETH9/WETH10: Predeployed at canonical addresses"

if [ $TOTAL_FOUND -eq 0 ]; then
    log_error "❌ No deployment addresses found"
    echo "  All contracts need to be deployed"
elif [ $TOTAL_FOUND -lt 3 ]; then
    log_warn "⚠️  Partial deployment"
    echo "  Some contracts still need deployment"
else
    log_success "✅ All contracts have addresses configured"
fi
