export type TrafficAlertEvent = {
  level: "info" | "warning" | "error";
  action: string;
  surface: string;
  actorType: "SYSTEM" | "AGENT" | "USER";
  actorId: string;
  taskId?: string;
  message: string;
  metadata?: Record<string, unknown>;
};

const TRAFFIC_WEBHOOK_URL = process.env.VIBEWORK_TRAFFIC_WEBHOOK_URL?.trim();

export async function sendTrafficAlert(event: TrafficAlertEvent): Promise<void> {
  if (!TRAFFIC_WEBHOOK_URL) return;

  const payload = {
    platform: "agent-bounty-protocol",
    created_at: new Date().toISOString(),
    ...event,
  };

  try {
    await fetch(TRAFFIC_WEBHOOK_URL, {
      method: "POST",
      headers: {
        "content-type": "application/json",
        "x-trace-source": "agent-bounty-protocol",
      },
      body: JSON.stringify(payload),
      signal: AbortSignal.timeout(3000),
    });
  } catch {
    // Avoid affecting request flow if webhook fails.
  }
}
