#!/usr/bin/env bash
# Generate CCIP Status Report
# Task 141: Create CCIP Status Report Script
# Usage: ./generate-ccip-status-report.sh [output_file]

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "$PROJECT_ROOT/scripts/lib/address-inventory.sh"
source "$PROJECT_ROOT/scripts/lib/ccip-destinations.sh"

load_explorer_runtime_env

# Configuration
RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
OUTPUT_FILE="${1:-docs/CCIP_STATUS_REPORT_$(date +%Y%m%d_%H%M%S).md}"

# Create report
{
    echo "# CCIP Status Report"
    echo ""
    echo "**Date**: $(date)"
    echo "**Network**: ChainID 138"
    echo "**RPC URL**: $RPC_URL"
    echo ""
    echo "---"
    echo ""
    echo "## Executive Summary"
    echo ""
    
    # Router status
    echo "### CCIP Router"
    ROUTER="$(resolve_address_value CCIP_ROUTER_ADDRESS CCIP_ROUTER_ADDRESS 0x42DAb7b888Dd382bD5Adcf9E038dBF1fD03b4817)"
    ROUTER_BYTECODE=$(cast code "$ROUTER" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    if [ -n "$ROUTER_BYTECODE" ] && [ "$ROUTER_BYTECODE" != "0x" ]; then
        echo "- **Status**: ✅ Deployed"
        echo "- **Address**: $ROUTER"
    else
        echo "- **Status**: ❌ Not Found"
    fi
    echo ""
    
    # Sender status
    echo "### CCIP Sender"
    SENDER="0x105F8A15b819948a89153505762444Ee9f324684"
    SENDER_BYTECODE=$(cast code "$SENDER" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    if [ -n "$SENDER_BYTECODE" ] && [ "$SENDER_BYTECODE" != "0x" ]; then
        echo "- **Status**: ✅ Deployed"
        echo "- **Address**: $SENDER"
    else
        echo "- **Status**: ❌ Not Found"
    fi
    echo ""
    
    # Bridge status
    echo "### Bridge Contracts"
    WETH9_BRIDGE="$(resolve_address_value CCIPWETH9_BRIDGE CCIPWETH9_BRIDGE 0xcacfd227A040002e49e2e01626363071324f820a)"
    WETH10_BRIDGE="$(resolve_address_value CCIPWETH10_BRIDGE CCIPWETH10_BRIDGE 0xe0E93247376aa097dB308B92e6Ba36bA015535D0)"
    
    WETH9_BRIDGE_BYTECODE=$(cast code "$WETH9_BRIDGE" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    WETH10_BRIDGE_BYTECODE=$(cast code "$WETH10_BRIDGE" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    
    if [ -n "$WETH9_BRIDGE_BYTECODE" ] && [ "$WETH9_BRIDGE_BYTECODE" != "0x" ]; then
        echo "- **WETH9 Bridge**: ✅ Deployed ($WETH9_BRIDGE)"
    else
        echo "- **WETH9 Bridge**: ❌ Not Found"
    fi
    
    if [ -n "$WETH10_BRIDGE_BYTECODE" ] && [ "$WETH10_BRIDGE_BYTECODE" != "0x" ]; then
        echo "- **WETH10 Bridge**: ✅ Deployed ($WETH10_BRIDGE)"
    else
        echo "- **WETH10 Bridge**: ❌ Not Found"
    fi
    echo ""
    
    # Destination configuration
    echo "### Bridge Destination Configuration"
    TOTAL_DESTINATIONS=$(ccip_destination_count)
    WETH9_CONFIGURED=0
    WETH10_CONFIGURED=0
    
    while IFS=$'\t' read -r _chain_name SELECTOR _weth9 _weth10 _rpc_url; do
        
        DEST_WETH9=$(cast call "$WETH9_BRIDGE" "destinations(uint64)" "$SELECTOR" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
        DEST_WETH9_CLEAN=$(echo "$DEST_WETH9" | grep -oE "^0x[0-9a-fA-F]{40}$" | head -1 || echo "")
        if [ -n "$DEST_WETH9_CLEAN" ] && ! echo "$DEST_WETH9_CLEAN" | grep -qE "^0x0+$"; then
            ((WETH9_CONFIGURED++)) || true
        fi
        
        DEST_WETH10=$(cast call "$WETH10_BRIDGE" "destinations(uint64)" "$SELECTOR" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
        DEST_WETH10_CLEAN=$(echo "$DEST_WETH10" | grep -oE "^0x[0-9a-fA-F]{40}$" | head -1 || echo "")
        if [ -n "$DEST_WETH10_CLEAN" ] && ! echo "$DEST_WETH10_CLEAN" | grep -qE "^0x0+$"; then
            ((WETH10_CONFIGURED++)) || true
        fi
    done < <(ccip_destination_rows)
    
    echo "- **WETH9 Bridge**: $WETH9_CONFIGURED/$TOTAL_DESTINATIONS destinations configured"
    echo "- **WETH10 Bridge**: $WETH10_CONFIGURED/$TOTAL_DESTINATIONS destinations configured"
    echo ""
    
    # Token status
    echo "### Token Contracts"
    WETH9="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
    WETH10="0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f"
    
    WETH9_BYTECODE=$(cast code "$WETH9" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    WETH10_BYTECODE=$(cast code "$WETH10" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    
    if [ -n "$WETH9_BYTECODE" ] && [ "$WETH9_BYTECODE" != "0x" ]; then
        echo "- **WETH9**: ✅ Deployed ($WETH9)"
    else
        echo "- **WETH9**: ❌ Not Found"
    fi
    
    if [ -n "$WETH10_BYTECODE" ] && [ "$WETH10_BYTECODE" != "0x" ]; then
        echo "- **WETH10**: ✅ Deployed ($WETH10)"
    else
        echo "- **WETH10**: ❌ Not Found"
    fi
    echo ""
    
    echo "---"
    echo ""
    echo "## Detailed Status"
    echo ""
    echo "### System Health"
    echo ""
    echo "Run comprehensive verification:"
    echo "\`\`\`bash"
    echo "./scripts/verify-complete-ccip-setup.sh"
    echo "\`\`\`"
    echo ""
    echo "### Next Steps"
    echo ""
    if [ $WETH9_CONFIGURED -lt $TOTAL_DESTINATIONS ] || [ $WETH10_CONFIGURED -lt $TOTAL_DESTINATIONS ]; then
        echo "1. Configure missing bridge destinations"
        echo "2. Verify configuration"
        echo "3. Test bridge operations"
    else
        echo "1. Test bridge operations"
        echo "2. Monitor system health"
        echo "3. Review configuration"
    fi
    echo ""
    echo "---"
    echo ""
    echo "**Report Generated**: $(date)"
    
} > "$OUTPUT_FILE"

echo "Status report generated: $OUTPUT_FILE"
