import { MailerService } from '@nestjs-modules/mailer';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { MailData } from './interfaces/mail-data.interface';

@Injectable()
export class MailService {
  constructor(
    private mailerService: MailerService,
    private configService: ConfigService,
  ) {}

  async userSignUp(
    mailData: MailData<{ hash: string; firstName?: string | null }>,
  ) {
    const actionTitle = 'E-Mail bestätigen';
    const url = `${this.configService.get('app.frontendDomain')}/confirm-email/${
      mailData.data.hash
    }`;

    await this.mailerService.sendMail({
      to: mailData.to,
      subject: 'Bestätige deine E-Mail-Adresse',
      text: `${url} ${actionTitle}`,
      template: 'activation',
      context: {
        title: 'Bestätige deine E-Mail-Adresse',
        year: new Date().getFullYear(),
        firstName: mailData.data.firstName,
        url,
        actionTitle,
      },
    });
  }

  async forgotPassword(
    mailData: MailData<{ hash: string; firstName?: string | null }>,
  ) {
    const actionTitle = 'Passwort zurücksetzen';
    const url = `${this.configService.get('app.frontendDomain')}/password-change/${
      mailData.data.hash
    }`;

    await this.mailerService.sendMail({
      to: mailData.to,
      subject: actionTitle,
      text: `${url} ${actionTitle}`,
      template: 'reset-password',
      context: {
        title: actionTitle,
        year: new Date().getFullYear(),
        firstName: mailData.data.firstName,
        url,
        actionTitle,
      },
    });
  }
}
