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 InvitationEmail {
  to: string;
  host: string;

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

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

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

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

      const appTitle = getNotification('app.title');
      const signupUrl = `${this.host}&invitation=true`;

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

export default InvitationEmail;
