import { LeadData, AdminConfig } from '../types'; export const crmService = { /** * Syncs a lead to the external CRM system defined in AdminConfig. * Uses a mock implementation if no URL is provided or for demo purposes. */ syncLead: async (lead: LeadData, config: AdminConfig): Promise<{ success: boolean; crmId?: string }> => { // 1. Check if CRM integration is enabled if (!config.crmApiUrl) { console.log('CRM Sync skipped: No API URL configured'); return { success: false }; } try { console.log(`Syncing lead ${lead.name} to ${config.crmApiUrl}...`); // 2. Simulate Network Request // In production, use: await fetch(config.crmApiUrl, { method: 'POST', headers: { 'Authorization': `Bearer ${config.crmApiKey}` }, body: JSON.stringify(lead) }); await new Promise(resolve => setTimeout(resolve, 1000)); // Mock latency // 3. Return Mock Success return { success: true, crmId: `CRM-${Date.now()}` }; } catch (error) { console.error('CRM Sync Failed:', error); return { success: false }; } }, /** * Generates a CSV/XML string compatible with LiaArk (Generic ERP format) */ exportToLiaArk: (leads: LeadData[]): string => { const header = "Name,Phone,Service,Date,Status,Source\n"; const rows = leads.map(l => `${l.name},${l.phone},${l.service},${l.date || ''},${l.status},AI-Fitting` ).join("\n"); return header + rows; } };