#!/usr/bin/env bash
# Monitor LINK Fees and Balances
# Usage: ./monitor-fees.sh [alert_threshold_link]

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"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

load_explorer_runtime_env

# Configuration
RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
LINK_TOKEN="$(resolve_address_value LINK_TOKEN LINK_TOKEN 0x326C977E6efc84E512bB9C30f76E30c160eD06FB)"
WETH9_BRIDGE="$(resolve_address_value CCIPWETH9_BRIDGE CCIPWETH9_BRIDGE 0xcacfd227A040002e49e2e01626363071324f820a)"
WETH10_BRIDGE="$(resolve_address_value CCIPWETH10_BRIDGE CCIPWETH10_BRIDGE 0xe0E93247376aa097dB308B92e6Ba36bA015535D0)"
ALERT_THRESHOLD="${1:-1.0}"  # Default 1 LINK

if [ -z "${PRIVATE_KEY:-}" ]; then
    log_error "PRIVATE_KEY not available in effective environment"
    exit 1
fi

ACCOUNT=$(cast wallet address "$PRIVATE_KEY" 2>/dev/null || echo "")
if [ -z "$ACCOUNT" ]; then
    log_error "Could not derive address from PRIVATE_KEY"
    exit 1
fi

log_info "========================================="
log_info "Fee Monitoring"
log_info "========================================="
log_info ""
log_info "Alert Threshold: $ALERT_THRESHOLD LINK"
log_info ""

# Check LINK token contract
LINK_CODE=$(cast code "$LINK_TOKEN" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -z "$LINK_CODE" ] || [ "$LINK_CODE" = "0x" ]; then
    log_error "LINK token contract not deployed"
    exit 1
fi

# Check account LINK balance
ACCOUNT_LINK=$(cast call "$LINK_TOKEN" "balanceOf(address)" "$ACCOUNT" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
ACCOUNT_LINK_ETH=$(cast --from-wei "$ACCOUNT_LINK" ether 2>/dev/null || echo "0")

log_info "Account LINK Balance: $ACCOUNT_LINK_ETH LINK"
if (( $(echo "$ACCOUNT_LINK_ETH < $ALERT_THRESHOLD" | bc -l 2>/dev/null || echo 1) )); then
    log_warn "⚠ Account LINK balance below threshold"
fi

# Check WETH9 Bridge LINK balance
WETH9_LINK=$(cast call "$LINK_TOKEN" "balanceOf(address)" "$WETH9_BRIDGE" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
WETH9_LINK_ETH=$(cast --from-wei "$WETH9_LINK" ether 2>/dev/null || echo "0")

log_info "WETH9 Bridge LINK Balance: $WETH9_LINK_ETH LINK"
if (( $(echo "$WETH9_LINK_ETH < $ALERT_THRESHOLD" | bc -l 2>/dev/null || echo 1) )); then
    log_warn "⚠ WETH9 Bridge LINK balance below threshold"
fi

# Check WETH10 Bridge LINK balance
WETH10_LINK=$(cast call "$LINK_TOKEN" "balanceOf(address)" "$WETH10_BRIDGE" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
WETH10_LINK_ETH=$(cast --from-wei "$WETH10_LINK" ether 2>/dev/null || echo "0")

log_info "WETH10 Bridge LINK Balance: $WETH10_LINK_ETH LINK"
if (( $(echo "$WETH10_LINK_ETH < $ALERT_THRESHOLD" | bc -l 2>/dev/null || echo 1) )); then
    log_warn "⚠ WETH10 Bridge LINK balance below threshold"
fi

# Summary
log_info ""
log_info "========================================="
log_info "Summary"
log_info "========================================="
log_info ""

ALERTS=0
if (( $(echo "$ACCOUNT_LINK_ETH < $ALERT_THRESHOLD" | bc -l 2>/dev/null || echo 1) )); then
    ((ALERTS++)) || true
fi
if (( $(echo "$WETH9_LINK_ETH < $ALERT_THRESHOLD" | bc -l 2>/dev/null || echo 1) )); then
    ((ALERTS++)) || true
fi
if (( $(echo "$WETH10_LINK_ETH < $ALERT_THRESHOLD" | bc -l 2>/dev/null || echo 1) )); then
    ((ALERTS++)) || true
fi

if [ $ALERTS -eq 0 ]; then
    log_success "✓ All LINK balances above threshold"
    exit 0
else
    log_warn "⚠ $ALERTS balance(s) below threshold"
    log_info ""
    log_info "Actions Required:"
    if (( $(echo "$ACCOUNT_LINK_ETH < $ALERT_THRESHOLD" | bc -l 2>/dev/null || echo 1) )); then
        log_info "  - Transfer LINK to account: $ACCOUNT"
    fi
    if (( $(echo "$WETH9_LINK_ETH < $ALERT_THRESHOLD" | bc -l 2>/dev/null || echo 1) )); then
        log_info "  - Transfer LINK to WETH9 Bridge: $WETH9_BRIDGE"
    fi
    if (( $(echo "$WETH10_LINK_ETH < $ALERT_THRESHOLD" | bc -l 2>/dev/null || echo 1) )); then
        log_info "  - Transfer LINK to WETH10 Bridge: $WETH10_BRIDGE"
    fi
    exit 1
fi
