import fs from 'fs';
import path from 'path';
import handlebars from 'handlebars';

const __dirname = path.resolve();

export const loadEmailTemplateFromFile = (
  templateName: string,
  replacements: Record<string, string | number>,
): string => {
  const filePath = path.join(
    __dirname,
    '/src/shared/emailTemplate/',
    `${templateName}.html`,
  );
  const templateSource = fs.readFileSync(filePath, 'utf-8');
  const template = handlebars.compile(templateSource);
  return template(replacements);
};

export const loadEmailTemplateFromTemplate = (
  templateSource: string,
  replacements: Record<string, string | number>,
): string => {
  const template = handlebars.compile(templateSource);
  return template(replacements);
};

type EmailWithAttachmentsOptions = {
  to: string;
  cc?: string;
  subject: string;
  html: string;
  attachments?: {
    filename: string;
    path: string;
  }[];
};
