"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_assignment_seeds_exports = {};
__export(whatsapp_template_assignment_seeds_exports, {
  whatsappTemplateAssignmentsSeeder: () => whatsappTemplateAssignmentsSeeder
});
module.exports = __toCommonJS(whatsapp_template_assignment_seeds_exports);
var import_client = require("@prisma/pg/client");
var import_fs = __toESM(require("fs"));
const prisma = new import_client.PrismaClient();
const whatsappTemplateAssignmentSeeder = JSON.parse(import_fs.default.readFileSync(__dirname + "/json/whatsapp-template-assignment.json", "utf-8"));
async function whatsappTemplateAssignmentsSeeder(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 datas = whatsappTemplateAssignmentSeeder;
    for (const data of datas) {
      const whatsappTemplateProcess = await prisma.notificationTemplateProcess.findFirst({
        where: {
          slug: data.notification_template_process_slug,
          company_id
        }
      });
      if (!whatsappTemplateProcess) {
        console.error(
          `Whatsapp template process with slug "${data.notification_template_process_slug}" not found for company_id ${company_id}.`
        );
        continue;
      }
      let template_id = null;
      if (data.name) {
        const template = await prisma.notificationTemplate.findFirst({
          where: {
            name: data.name,
            company_id
          }
        });
        if (template) {
          template_id = template.id;
        } else {
          console.error(
            `User template with slug "${data.name}" not found for company_id ${company_id}.`
          );
          continue;
        }
      }
      let notification_to = `${data.notification_to}` || null;
      if (data.recipient_type === "tag") {
        notification_to = null;
        if (data.notification_to) {
          const template = await prisma.notificationTemplateRecipientTag.findFirst({
            where: {
              slug: `${data.notification_to}`,
              company_id
            }
          });
          if (template) {
            notification_to = `${template.id}`;
          } else {
            console.error(
              `Email template recipient tag with slug "${data.notification_to}" not found for company_id ${company_id}.`
            );
            continue;
          }
        }
      }
      const whatsapp_template_process_id = whatsappTemplateProcess.id;
      const existingAssignment = await prisma.notificationTemplateAssignment.findFirst(
        {
          where: {
            notification_template_process_id: whatsapp_template_process_id,
            template_id: template_id || 0,
            recipient_type: data.recipient_type,
            notification_to,
            company_id
          }
        }
      );
      if (!existingAssignment && template_id) {
        await prisma.notificationTemplateAssignment.create({
          data: {
            notification_template_process_id: whatsapp_template_process_id,
            template_id,
            recipient_type: data.recipient_type,
            notification_to,
            company_id,
            created_by,
            created_at: /* @__PURE__ */ new Date()
          }
        });
        console.log(
          `Whatsapp Template Assignment with whatsapp_template_process_id "${whatsapp_template_process_id}" created for company_id ${company_id}.`
        );
      } else {
        console.log(
          `Whatsapp Template Assignment with whatsapp_template_process_id "${whatsapp_template_process_id}" already exists for company_id ${company_id}.`
        );
      }
    }
  } catch (error) {
    console.error("Error creating whatsapp template assignments:", error);
    throw error;
  } finally {
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  whatsappTemplateAssignmentsSeeder
});
