import type { LeadPayload } from '../types';

const config = window.noon_audit_config;

export async function submitLead(payload: LeadPayload): Promise<{ success: boolean; lead_id?: number }> {
  // In dev mode (no REST URL), just log and return mock.
  if (!config.restUrl) {
    console.log('[Dev] Lead submitted:', payload);
    return { success: true, lead_id: 999 };
  }

  const res = await fetch(`${config.restUrl}/leads`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-WP-Nonce': config.nonce,
    },
    body: JSON.stringify(payload),
  });

  if (!res.ok) {
    throw new Error(`Failed to submit lead: ${res.statusText}`);
  }

  return res.json();
}
