import type { MonitorEnv } from './types.js';

/**
 * Ping an external uptime/heartbeat service (e.g. healthchecks.io). If the worker
 * stops entirely, the external service alerts — the only mechanism that survives
 * total in-CF death. No-op when OPS_HEARTBEAT_URL is unset. Never throws.
 */
export async function pingDeadMan(env: MonitorEnv): Promise<void> {
  if (!env.OPS_HEARTBEAT_URL) return;
  try {
    await fetch(env.OPS_HEARTBEAT_URL, { method: 'GET' });
  } catch (err) {
    console.error(
      '[ops-monitor] dead-man ping failed',
      err instanceof Error ? err.name : 'unknown',
    );
  }
}
