// WhatsApp send transport. Currently a console-only stub — when a real
// provider (Twilio, WhatsApp Cloud API, etc.) is wired in, swap the
// implementation here and every send call site keeps working unchanged.
//
// The stub deliberately returns success so the existing send hooks can
// integration-test their wiring without needing a provider configured.
// Set WHATSAPP_LOG=false to silence the dev console output.

export interface WhatsAppMessage {
  /** Destination, in international format. Leading + is optional — the
   *  transport normalises to digits-only before dispatch. */
  to: string;
  body: string;
  /** Optional image attachment. When set, the recipient sees the image
   *  rendered inline by their WhatsApp client (with the `body` as caption)
   *  — no raw URL appears in the text. The stub just logs the URL on a
   *  separate line; a real provider call will translate this into the
   *  provider's media-attachment API. */
  mediaUrl?: string;
}

const isTransportConfigured = (): boolean => {
  // Placeholder — flip true when the real provider env vars are present.
  return false;
};

export async function sendWhatsApp(msg: WhatsAppMessage): Promise<{ ok: boolean; skipped?: boolean; reason?: string }> {
  const digits = (msg.to || '').replace(/\D+/g, '');
  if (!digits) {
    return { ok: false, skipped: true, reason: 'Missing destination number' };
  }

  if (!isTransportConfigured()) {
    if (process.env.WHATSAPP_LOG !== 'false') {
      console.log(`[DEV WA] To: +${digits}`);
      console.log(`[DEV WA] Body: ${msg.body}`);
      if (msg.mediaUrl) console.log(`[DEV WA] Media: ${msg.mediaUrl}`);
    }
    return { ok: true, skipped: true, reason: 'transport-not-configured' };
  }

  // TODO: real provider integration. For now we treat the no-provider case
  // as success-with-skip so the caller can keep its happy-path logic
  // unchanged once a provider is wired in.
  return { ok: true };
}
