interface ReminderEmailTemplateProps { invoice: { invoiceNumber: string; issueDate: Date; dueDate: Date; totalAmount: number; currency?: string | null; client: { name: string; email: string | null }; business?: { name: string; nickname?: string | null; email?: string | null; } | null; }; customMessage?: string; userName?: string; userEmail?: string; } export function generateReminderEmailTemplate({ invoice, customMessage, userName, userEmail, }: ReminderEmailTemplateProps): { html: string; text: string; subject: string } { const formatDate = (date: Date) => new Intl.DateTimeFormat("en-US", { year: "numeric", month: "long", day: "numeric" }).format( new Date(date), ); const formatCurrency = (amount: number) => new Intl.NumberFormat("en-US", { style: "currency", currency: invoice.currency ?? "USD", }).format(amount); const senderName = invoice.business?.name ? invoice.business.nickname ? `${invoice.business.name} (${invoice.business.nickname})` : invoice.business.name : userName ?? "Your service provider"; const isOverdue = new Date(invoice.dueDate) < new Date(); const subject = `Payment Reminder: Invoice ${invoice.invoiceNumber} — ${formatCurrency(invoice.totalAmount)}`; const defaultMessage = isOverdue ? `This is a friendly reminder that Invoice ${invoice.invoiceNumber} for ${formatCurrency(invoice.totalAmount)} was due on ${formatDate(invoice.dueDate)} and remains outstanding. Please arrange payment at your earliest convenience.` : `This is a friendly reminder that Invoice ${invoice.invoiceNumber} for ${formatCurrency(invoice.totalAmount)} is due on ${formatDate(invoice.dueDate)}. Please ensure payment is arranged before the due date.`; const bodyMessage = customMessage ?? defaultMessage; const html = ` Payment Reminder

${senderName}

${userEmail ? `

${userEmail}

` : ""}
${isOverdue ? "OVERDUE" : "PAYMENT DUE"}

Dear ${invoice.client.name},

${bodyMessage}

Invoice number ${invoice.invoiceNumber}
Issue date ${formatDate(invoice.issueDate)}
Due date ${formatDate(invoice.dueDate)}
Amount due ${formatCurrency(invoice.totalAmount)}

If you have already made payment, please disregard this notice. Thank you for your business.

Sent by ${senderName} · Powered by beenvoice

`; const text = `Payment Reminder from ${senderName} Dear ${invoice.client.name}, ${bodyMessage} Invoice: ${invoice.invoiceNumber} Issue date: ${formatDate(invoice.issueDate)} Due date: ${formatDate(invoice.dueDate)} Amount due: ${formatCurrency(invoice.totalAmount)} If you have already made payment, please disregard this notice. Thank you for your business. — ${senderName}`; return { html, text, subject }; }