#!/usr/bin/env bash
# Generate comprehensive bridge activity report
# Usage: ./generate-bridge-report.sh [daily|weekly|monthly]

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
SOURCE_PROJECT="/home/intlc/projects/smom-dbis-138"

source "$SOURCE_PROJECT/.env" 2>/dev/null || true

RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
REPORT_TYPE="${1:-daily}"
REPORT_DIR="$PROJECT_ROOT/reports"
DATE=$(date +%Y%m%d)

mkdir -p "$REPORT_DIR"

generate_report() {
    local report_file="$REPORT_DIR/bridge-report-$REPORT_TYPE-$DATE.md"
    
    {
        echo "# Bridge Activity Report - $REPORT_TYPE"
        echo "Generated: $(date -u +"%Y-%m-%d %H:%M:%S UTC")"
        echo ""
        echo "## System Status"
        echo ""
        
        # RPC Status
        if cast block-number --rpc-url "$RPC_URL" >/dev/null 2>&1; then
            echo "✅ RPC: Accessible"
            echo "   Block Number: $(cast block-number --rpc-url "$RPC_URL")"
        else
            echo "❌ RPC: Not accessible"
        fi
        echo ""
        
        # Bridge Contracts
        echo "## Bridge Contracts"
        echo ""
        WETH9_BRIDGE="${CCIPWETH9_BRIDGE_CHAIN138:-0x89dd12025bfCD38A168455A44B400e913ED33BE2}"
        WETH10_BRIDGE="${CCIPWETH10_BRIDGE_CHAIN138:-0xe0E93247376aa097dB308B92e6Ba36bA015535D0}"
        
        if cast code "$WETH9_BRIDGE" --rpc-url "$RPC_URL" >/dev/null 2>&1; then
            echo "✅ WETH9 Bridge: $WETH9_BRIDGE"
        else
            echo "❌ WETH9 Bridge: Not found"
        fi
        
        if cast code "$WETH10_BRIDGE" --rpc-url "$RPC_URL" >/dev/null 2>&1; then
            echo "✅ WETH10 Bridge: $WETH10_BRIDGE"
        else
            echo "❌ WETH10 Bridge: Not found"
        fi
        echo ""
        
        # Destination Chains
        echo "## Destination Chains"
        echo ""
        declare -A CHAINS=(
            ["BSC"]="11344663589394136015"
            ["Polygon"]="4051577828743386545"
            ["Avalanche"]="6433500567565415381"
            ["Base"]="15971525489660198786"
            ["Arbitrum"]="4949039107694359620"
            ["Optimism"]="3734403246176062136"
            ["Ethereum"]="5009297550715157269"
        )
        
        for chain in "${!CHAINS[@]}"; do
            selector="${CHAINS[$chain]}"
            result=$(cast call "$WETH9_BRIDGE" "destinations(uint64)" "$selector" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
            if [ -n "$result" ] && ! echo "$result" | grep -q "0x0000000000000000000000000000000000000000$"; then
                echo "✅ $chain: Configured"
            else
                echo "❌ $chain: Not configured"
            fi
        done
        echo ""
        
        # Balances
        echo "## Balances"
        echo ""
        DEPLOYER=$(cast wallet address --private-key "$PRIVATE_KEY" 2>/dev/null || echo "")
        if [ -n "$DEPLOYER" ]; then
            ETH_BAL=$(cast balance "$DEPLOYER" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
            ETH_BAL_ETH=$(echo "scale=4; $ETH_BAL / 1000000000000000000" | bc 2>/dev/null || echo "0")
            echo "Deployer: $DEPLOYER"
            echo "ETH Balance: $ETH_BAL_ETH ETH"
        fi
        echo ""
        
        # Gas Prices
        echo "## Current Gas Prices"
        echo ""
        GAS_PRICE=$(cast gas-price --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
        GAS_GWEI=$(echo "scale=2; $GAS_PRICE / 1000000000" | bc 2>/dev/null || echo "0")
        echo "Current: $GAS_GWEI gwei"
        echo ""
        
        echo "---"
        echo "Report generated by bridge monitoring system"
    } > "$report_file"
    
    echo "Report generated: $report_file"
    cat "$report_file"
}

generate_report

