import Handlebars from "handlebars";
import path from "path";
import fs from "fs/promises";

export async function renderTemplate(
  templateName: string,
  context: Record<string, any>,
) {
  try {
    const templatePath = path.join(
      process.cwd(),
      "templates",
      `${templateName}.hbs`,
    );
    const templateContent = await fs.readFile(templatePath, "utf8");
    const template = Handlebars.compile(templateContent);
    return template(context);
  } catch (error) {
    console.error(`Error rendering template ${templateName}:`, error);
    return `Error rendering template ${templateName}`;
  }
}
