import { readFile } from "fs/promises";
import { join } from "path";

const TEMPLATE_PATH = join(
  "src",
  "app",
  "api",
  "v1",
  "marketingMaterialSupplies",
  "templates",
  "data-template.txt",
);

export async function readAndModifyTemplate(data: any): Promise<string> {
  let content = await readFile(TEMPLATE_PATH, "utf-8");

  const newData = {
    ...data,
  };

  if (data.template === "v1") {
    newData.capButtonBackgroundColor = "COLORS.principal_color";
    newData.aboutUsBackgroundColor = "COLORS.dark_1";
    newData.codeListBackgroundColor = "COLORS.light_1";
    newData.codeListColor = "COLORS.principal_color";
    newData.pastPerformanceBackgroundColor = "COLORS.dark_2";
  }

  if (data.template === "v2") {
    newData.capButtonBackgroundColor = "COLORS.secondary_color";
    newData.aboutUsBackgroundColor = "COLORS.light_1";
    newData.codeListBackgroundColor = "COLORS.principal_color";
    newData.codeListColor = '"white"';
    newData.pastPerformanceBackgroundColor = "COLORS.light_2";
  }

  Object.keys(newData).forEach((key) => {
    if (typeof newData[key] !== "string") return;

    content = content.replaceAll(`{{ ${key} }}`, newData[key]);
  });

  return content;
}
