'use client'; import { useState, useCallback } from 'react'; import { api } from '@/lib/api/client'; interface Message { id: string; type: 'SMS' | 'EMAIL'; direction: 'inbound' | 'outbound'; body?: string; subject?: string; status: string; dateAdded: string; } interface Conversation { id: string; contactId: string; contactName?: string; lastMessageBody?: string; lastMessageDate?: string; unreadCount: number; type: 'SMS' | 'EMAIL'; } export function useConversations() { const [conversations, setConversations] = useState([]); const [messages, setMessages] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const fetchConversations = useCallback(async (params?: { limit?: number; status?: string; contactId?: string }) => { setLoading(true); setError(null); try { const result = await api.conversations.getAll(params); setConversations(result.data || []); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to fetch conversations'); } finally { setLoading(false); } }, []); const fetchMessages = useCallback(async (conversationId: string, limit = 50) => { setLoading(true); setError(null); try { const result = await api.conversations.getMessages(conversationId, limit); setMessages(result.data || []); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to fetch messages'); } finally { setLoading(false); } }, []); const sendSMS = async (contactId: string, message: string) => { const result = await api.conversations.sendSMS(contactId, message); return result; }; const sendEmail = async (contactId: string, subject: string, htmlBody: string) => { const result = await api.conversations.sendEmail(contactId, subject, htmlBody); return result; }; const markAsRead = async (id: string) => { await api.conversations.markAsRead(id); setConversations(prev => prev.map(c => c.id === id ? { ...c, unreadCount: 0 } : c) ); }; const deleteConversation = async (id: string) => { await api.conversations.delete(id); setConversations(prev => prev.filter(c => c.id !== id)); }; return { conversations, messages, loading, error, fetchConversations, fetchMessages, sendSMS, sendEmail, markAsRead, deleteConversation, }; }