import logger from '@/shared/logger';
import { promises as fs } from 'fs';
import path from 'path';
import { getNotification } from '@/shared/notifications/helpers';

const __dirname = import.meta.dirname;

class PasswordResetEmail {
  to: string;
  link: string;

  constructor(to: string, link: string) {
    this.to = to;
    this.link = link;
  }

  get subject(): string {
    return getNotification(
      'emails.passwordReset.subject',
      getNotification('app.title'),
    );
  }

  async html(): Promise<string> {
    try {
      const templatePath = path.join(
        __dirname,
        '../../email/htmlTemplates/passwordReset/passwordResetEmail.html',
      );

      const template = await fs.readFile(templatePath, 'utf8');

      const appTitle = getNotification('app.title');
      const resetUrl = this.link;
      const accountName = this.to;

      return template
        .replace(/{appTitle}/g, appTitle)
        .replace(/{resetUrl}/g, resetUrl)
        .replace(/{accountName}/g, accountName);
    } catch (error) {
      logger.error('Error generating password reset email HTML:', error);
      throw error;
    }
  }
}

export default PasswordResetEmail;
