#!/usr/bin/env bash
# Automated monitoring and alerting for bridge system
# Usage: Run via cron every 5 minutes

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}"
LOG_DIR="$PROJECT_ROOT/logs"
ALERT_LOG="$LOG_DIR/alerts-$(date +%Y%m%d).log"

mkdir -p "$LOG_DIR"

# Alert function
alert() {
    local level="$1"
    local message="$2"
    echo "[$(date -u +"%Y-%m-%d %H:%M:%S UTC")] [$level] $message" >> "$ALERT_LOG"
    
    # Critical alerts can trigger notifications here
    if [ "$level" = "CRITICAL" ]; then
        # Add notification logic (email, Slack, etc.)
        echo "CRITICAL: $message"
    fi
}

# Check RPC health
check_rpc_health() {
    if ! cast block-number --rpc-url "$RPC_URL" >/dev/null 2>&1; then
        alert "CRITICAL" "RPC endpoint is not accessible: $RPC_URL"
        return 1
    fi
    return 0
}

# Check bridge contracts
check_bridge_contracts() {
    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
        alert "CRITICAL" "WETH9 Bridge contract not found: $WETH9_BRIDGE"
        return 1
    fi
    
    if ! cast code "$WETH10_BRIDGE" --rpc-url "$RPC_URL" >/dev/null 2>&1; then
        alert "CRITICAL" "WETH10 Bridge contract not found: $WETH10_BRIDGE"
        return 1
    fi
    
    return 0
}

# Check destination chains
check_destinations() {
    WETH9_BRIDGE="${CCIPWETH9_BRIDGE_CHAIN138:-0x89dd12025bfCD38A168455A44B400e913ED33BE2}"
    
    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 [ -z "$result" ] || echo "$result" | grep -q "0x0000000000000000000000000000000000000000$"; then
            alert "WARNING" "Destination chain $chain is not configured"
        fi
    done
}

# Check balances
check_balances() {
    DEPLOYER=$(cast wallet address --private-key "$PRIVATE_KEY" 2>/dev/null || echo "")
    if [ -z "$DEPLOYER" ]; then
        alert "WARNING" "Cannot determine deployer address"
        return
    fi
    
    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")
    
    if (( $(echo "$ETH_BAL_ETH < 0.1" | bc -l 2>/dev/null || echo 1) )); then
        alert "WARNING" "Low ETH balance: $ETH_BAL_ETH ETH"
    fi
}

# Main monitoring
main() {
    check_rpc_health || exit 1
    check_bridge_contracts || exit 1
    check_destinations
    check_balances
    
    echo "[$(date -u +"%Y-%m-%d %H:%M:%S UTC")] Health check completed"
}

main "$@"

