'use server'; import { getEmails, EmailMessage, EmailAddress } from '@/lib/services/email-service'; import { formatEmailForReplyOrForward } from '@/lib/utils/email-formatter'; /** * Server action to fetch emails */ export async function fetchEmails(userId: string, folder = 'INBOX', page = 1, perPage = 20, searchQuery = '') { try { const result = await getEmails(userId, folder, page, perPage, searchQuery); return { success: true, data: result }; } catch (error) { console.error('Error fetching emails:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to fetch emails' }; } } /** * Server action to format email for reply or forward operations * Uses the centralized email formatter */ export async function formatEmailServerSide( email: { id: string; from: string; fromName?: string; to: string; subject: string; content: string; cc?: string; date: string; }, type: 'reply' | 'reply-all' | 'forward' ) { try { // Convert the client email format to the server EmailMessage format const serverEmail: EmailMessage = { id: email.id, subject: email.subject, from: [ { name: email.fromName || email.from.split('@')[0], address: email.from } ], to: [ { name: '', address: email.to } ], cc: email.cc ? [ { name: '', address: email.cc } ] : undefined, date: new Date(email.date), flags: { seen: true, flagged: false, answered: false, deleted: false, draft: false }, content: email.content, hasAttachments: false, folder: 'INBOX', contentFetched: true }; // Use the centralized formatter const formatted = formatEmailForReplyOrForward(serverEmail, type); return { success: true, data: formatted }; } catch (error) { console.error('Error formatting email:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to format email' }; } } /** * Send an email from the server */ export async function sendEmailServerSide( userId: string, emailData: { to: string; cc?: string; bcc?: string; subject: string; body: string; attachments?: Array<{ name: string; content: string; type: string; }>; } ) { try { const { sendEmail } = await import('@/lib/services/email-service'); const result = await sendEmail(userId, emailData); return result; } catch (error) { console.error('Error sending email:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to send email' }; } }