import { EmailTemplate, PortfolioInvitationEmail, RecoverPasswordEmail } from '@/src/components/builder/contact/EmailTemplate';
import { Invitation } from '@prisma/client';
import { Resend } from 'resend';

export class EmailService {
  private static readonly resend = new Resend(process.env.RESEND_API_KEY);

  static async sendInvitations(invitations: Invitation[], portfolioName: string, portfolioOwner?: string) {
    if (process.env.NODE_ENV !== 'production') return;

    for (const invitation of invitations) {
      await this.resend.emails.send({
        from: 'noreply@portfolio-app.xyz',
        to: invitation.email,
        subject: 'Invitation to join a portfolio',
        react: PortfolioInvitationEmail({
          portfolioName,
          portfolioOwner: portfolioOwner ?? '',
          acceptLink: `${process.env.NEXT_PUBLIC_FRONTEND_URL}/register/${invitation.token}`,
        }),
      });
    }
  }

  static async sendPasswordReset(email: string, name: string, token: string) {
    if (process.env.NODE_ENV !== 'production') return;

    return await this.resend.emails.send({
      from: 'noreply@portfolio-app.xyz',
      to: email,
      subject: 'Password Reset',
      react: RecoverPasswordEmail({
        name,
        resetLink: `${process.env.NEXT_PUBLIC_FRONTEND_URL}/reset-password?token=${token}`,
      }),
    });
  }

  static async sendContactMessage(name: string, email: string, message: string) {
    if (process.env.NODE_ENV !== 'production') return;

    return await this.resend.emails.send({
      from: 'noreply@portfolio-app.xyz',
      to: email,
      replyTo: email,
      subject: 'Contact Message',
      react: EmailTemplate({
        name,
        email,
        message,
      }),
    });
  }
}