#!/usr/bin/env bash
# Set up monitoring and alerting for deployed contracts

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../lib/init.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
# Load .env via dotenv (RPC CR/LF trim). Fallback: raw source.
if [[ -f "$SCRIPT_DIR/../lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1090
  source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "${PROJECT_ROOT:-$REPO_ROOT}"
elif [[ -n "${PROJECT_ROOT:-}" && -f "$PROJECT_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$PROJECT_ROOT/.env"
  set +a
elif [[ -n "${REPO_ROOT:-}" && -f "$REPO_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$REPO_ROOT/.env"
  set +a
fi


log_info "=== Monitoring Setup Guide ==="

# Load environment
if [ -f "$PROJECT_ROOT/.env" ]; then
    source "$PROJECT_ROOT/.env"
fi

log_info "Monitoring Checklist:"
echo "1. Etherscan Alerts:"
echo "   • Set up alerts for contract events"
echo "   • Monitor transaction activity"
echo "   • Track contract interactions"
echo "2. Event Monitoring:"
echo "   • CrossChainTransferInitiated"
echo "   • CrossChainTransferCompleted"
echo "   • CrossChainTransferFailed"
echo "3. Balance Monitoring:"
echo "   • Monitor bridge LINK balances (for CCIP fees)"
echo "   • Monitor bridge WETH balances"
echo "   • Set up alerts for low balances"
echo "4. CCIP Message Tracking:"
echo "   • Monitor CCIP message status"
echo "   • Track message delivery times"
echo "   • Alert on failed messages"
echo "5. Recommended Tools:"
echo "   • Etherscan API for event monitoring"
echo "   • The Graph for indexing"
echo "   • Tenderly for transaction monitoring"
echo "   • Custom scripts with ethers.js"
log_warn "⚠️  Manual setup required"
