import { Resend } from "resend"; const resendApiKey = process.env.RESEND_API_KEY; const defaultFromAddress = process.env.RESEND_FROM_EMAIL ?? "AeThex OS "; const verifySupportEmail = process.env.VERIFY_SUPPORT_EMAIL ?? "support@aethex.biz"; const resendClient = resendApiKey ? new Resend(resendApiKey) : null; export const emailService = { get isConfigured() { return Boolean(resendClient); }, async sendVerificationEmail(params: { to: string; verificationUrl: string; fullName?: string | null; }) { if (!resendClient) { throw new Error("Email service is not configured. Set RESEND_API_KEY."); } const { to, verificationUrl, fullName } = params; const safeName = fullName?.trim() || "there"; const subject = "Verify your AeThex account"; const previewText = "Confirm your AeThex account to access the dashboard."; const html = `

Welcome to AeThex, ${safeName}!

Click the button below to verify your account and unlock your personal dashboard.

Verify my account

If the button does not work, paste this link into your browser:

${verificationUrl}


Didnt create an account? Please ignore this email or contact ${verifySupportEmail}.

`; const text = [ `Welcome to AeThex, ${safeName}!`, "", "Use the link below to verify your account:", verificationUrl, "", `If you didn't request this, contact us at ${verifySupportEmail}.`, ].join("\n"); await resendClient.emails.send({ from: defaultFromAddress, to, subject, html, text, headers: { "X-AeThex-Email": "verification", "X-Entity-Ref-ID": verificationUrl.slice(-24), }, tags: [{ name: "template", value: "auth-verification" }], reply_to: verifySupportEmail, }); }, async sendInviteEmail(params: { to: string; inviteUrl: string; inviterName?: string | null; message?: string | null; }) { if (!resendClient) { throw new Error("Email service is not configured. Set RESEND_API_KEY."); } const { to, inviteUrl, inviterName, message } = params; const safeInviter = inviterName?.trim() || "An AeThex member"; const subject = `${safeInviter} invited you to collaborate on AeThex`; const html = `

You're invited to AeThex

${safeInviter} sent you an invitation to connect and collaborate on AeThex.

${message ? `
${message}
` : ""}

Accept invitation

If the button does not work, paste this link into your browser:

${inviteUrl}

`; const text = [ `You're invited to AeThex by ${safeInviter}.`, message ? `\nMessage: ${message}` : "", "\nAccept here:", inviteUrl, ].join("\n"); await resendClient.emails.send({ from: defaultFromAddress, to, subject, html, text, headers: { "X-AeThex-Email": "invite" }, tags: [{ name: "template", value: "invite" }], reply_to: verifySupportEmail, }); }, };