import { apiDownload, apiGet, apiPost, apiPut } from "@/app/lib/clientApi"; import type { EventLeadCapturePayload, EventLeadEvent, EventLeadRow, EventLeadUpdatePayload, InterestOption, LeadMatchResult, SalespersonOption, } from "@/app/lib/types"; const BASE = "/api/event-leads"; const FILTERS = ["salespersonId", "temperature", "status", "interestId", "leadType", "attended"] as const; export type EventLeadFilters = Partial>; export interface EventLeadListParams extends EventLeadFilters { /** 1-based. */ page: number; pageSize: number; companyId?: number; } export const listEventLeadEvents = (companyId?: number) => apiGet(`${BASE}/events${companyId ? `?companyId=${companyId}` : ""}`); export const listEventLeadInterests = () => apiGet(`${BASE}/interest-options`); export const listEventSalespeople = (eventUuid: string) => apiGet(`${BASE}/${eventUuid}/salesperson-options`); export async function listEventLeads(eventUuid: string, params: EventLeadListParams): Promise<{ items: EventLeadRow[]; total: number }> { const qs = new URLSearchParams(); for (const key of FILTERS) if (params[key]) qs.set(key, params[key]); if (params.companyId) qs.set("companyId", String(params.companyId)); qs.set("page", String(Math.max(0, params.page - 1))); qs.set("size", String(params.pageSize)); const raw = await apiGet<{ content?: EventLeadRow[]; totalElements?: number } | null>(`${BASE}/${eventUuid}/leads?${qs}`); const items = Array.isArray(raw?.content) ? raw.content : []; return { items, total: typeof raw?.totalElements === "number" ? raw.totalElements : items.length }; } export const matchLead = (eventUuid: string, contact: { mobile: string | null; email: string | null }, companyId?: number) => apiPost(`${BASE}/${eventUuid}/match`, companyId ? { ...contact, companyId } : contact); export const captureEventLead = (eventUuid: string, payload: EventLeadCapturePayload) => apiPost(`${BASE}/${eventUuid}`, payload); export const getEventLead = (eventUuid: string, eventLeadUuid: string) => apiGet(`${BASE}/${eventUuid}/leads/${eventLeadUuid}`); export const updateEventLead = (eventUuid: string, eventLeadUuid: string, payload: EventLeadUpdatePayload) => apiPut(`${BASE}/${eventUuid}/leads/${eventLeadUuid}`, payload); export function exportEventLeads(eventUuid: string, filters: EventLeadFilters, companyId?: number) { const qs = new URLSearchParams(); for (const key of FILTERS) if (filters[key]) qs.set(key, filters[key]); if (companyId) qs.set("companyId", String(companyId)); return apiDownload(`${BASE}/${eventUuid}/export${qs.size ? `?${qs}` : ""}`, "event-leads.xlsx"); }