import { sendEmail } from '@/lib/mail' function formatTrialEnd(date: Date, locale: string): string { try { return new Intl.DateTimeFormat(locale, { day: 'numeric', month: 'long', year: 'numeric', }).format(date) } catch { return date.toISOString().slice(0, 10) } } /** * Best-effort reminder ~3 days before Stripe ends a trial. * Failures are logged by the caller; never throw to the webhook. */ export async function sendTrialEndingReminder(opts: { to: string name?: string | null trialEndsAt: Date billingUrl: string locale?: string }): Promise<{ success: boolean; error?: string }> { const locale = opts.locale?.startsWith('fr') ? 'fr' : 'en' const endLabel = formatTrialEnd(opts.trialEndsAt, locale === 'fr' ? 'fr-FR' : 'en-US') const greet = opts.name?.trim() ? opts.name.trim() : locale === 'fr' ? 'Bonjour' : 'Hi' const subject = locale === 'fr' ? 'Votre essai Memento se termine bientôt' : 'Your Memento trial is ending soon' const html = locale === 'fr' ? `
${greet},
Votre période d'essai Memento se termine le ${endLabel}.
Après cette date, votre abonnement démarrera automatiquement avec le moyen de paiement enregistré.
Pour gérer votre abonnement ou votre carte :
— L'équipe Memento
` : `${greet},
Your Memento trial ends on ${endLabel}.
After that date, your subscription will start automatically using the payment method on file.
To manage your subscription or card:
— The Memento team
` return sendEmail({ to: opts.to, subject, html }) }