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 EmailAddressVerificationEmail {
  to: string;
  link: string;

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

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

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

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

      const appTitle = getNotification('app.title');
      const signupUrl = this.link;

      return template
        .replace(/{appTitle}/g, appTitle)
        .replace(/{signupUrl}/g, signupUrl)
        .replace(/{to}/g, this.to);
    } catch (error) {
      logger.error(
        'Error generating address verification email HTML:',
        error,
      );
      throw error;
    }
  }
}

export default EmailAddressVerificationEmail;
