"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var whatsapp_template_seeds_exports = {};
__export(whatsapp_template_seeds_exports, {
  whatsappTemplatesSeeder: () => whatsappTemplatesSeeder
});
module.exports = __toCommonJS(whatsapp_template_seeds_exports);
var import_client = require("@prisma/pg/client");
var import_uuid = require("uuid");
var import_fs = __toESM(require("fs"));
const prisma = new import_client.PrismaClient();
const whatsappTemplateSeeder = JSON.parse(import_fs.default.readFileSync(__dirname + "/json/whatsapp-template.json", "utf-8"));
async function whatsappTemplatesSeeder(company_id) {
  try {
    let user = await prisma.user.findFirst({
      where: { company_id, role: "super_admin" }
    });
    if (!user) {
      console.log(
        `Menu seeder failed: admin user not available for company id ${company_id}`
      );
      return false;
    }
    const created_by = user.id;
    const company = await prisma.user.findFirst({
      where: { company_id }
    });
    if (!company) {
      console.log(`WARNING: Company with id ${company_id} does not exist.`);
      return;
    }
    const datas = whatsappTemplateSeeder;
    for (const data of datas) {
      const whatsappTemplateProcess = await prisma.notificationTemplateProcess.findFirst({
        where: {
          slug: data.whatsapp_template_process_slug,
          company_id
        }
      });
      if (!whatsappTemplateProcess) {
        console.log(
          `Email Template Process with slug "${data.whatsapp_template_process_slug}" not found for company_id ${company_id}. Skipping template creation.`
        );
        continue;
      }
      const existingTemplate = await prisma.notificationTemplate.findFirst({
        where: {
          slug: data.slug,
          company_id
        }
      });
      if (!existingTemplate) {
        try {
          await prisma.notificationTemplate.create({
            data: {
              uuid: (0, import_uuid.v4)(),
              slug: data.slug,
              name: data.name,
              message: data.message,
              notification_template_process_id: whatsappTemplateProcess.id,
              status_id: data.status_id,
              company_id,
              created_by
            }
          });
          console.log(
            `Created new Whatsapp Template: ${data.name} for company_id ${company_id}`
          );
        } catch (createError) {
          console.error(
            `Error creating Whatsapp template with slug "${data.slug}":`,
            createError
          );
        }
      } else {
        await prisma.notificationTemplate.update({
          data: {
            name: data.name,
            message: data.message,
            notification_template_process_id: whatsappTemplateProcess.id,
            status_id: data.status_id,
            company_id,
            updated_by: created_by
          },
          where: {
            id: existingTemplate.id
          }
        });
        console.log(`Email Template with slug "${data.slug}" with company_id ${company_id} has been updated.`);
      }
    }
  } catch (error) {
    console.error(`Error in seedEmailTemplates: ${error}`);
    throw error;
  } finally {
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  whatsappTemplatesSeeder
});
