"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_process_seeds_exports = {};
__export(whatsapp_template_process_seeds_exports, {
  whatsappTemplatesProcessSeeder: () => whatsappTemplatesProcessSeeder
});
module.exports = __toCommonJS(whatsapp_template_process_seeds_exports);
var import_client = require("@prisma/pg/client");
var import_fs = __toESM(require("fs"));
const prisma = new import_client.PrismaClient();
const whatsappTemplateProcessSeeder = JSON.parse(import_fs.default.readFileSync(__dirname + "/json/whatsapp-template-process.json", "utf-8"));
const whatsappRecipientsSeeder = JSON.parse(import_fs.default.readFileSync(__dirname + "/json/whatsapp-recipients.json", "utf-8"));
async function findOrCreateProcess(process, company_id) {
  try {
    let templateProcess = await prisma.notificationTemplateProcess.findUnique({
      where: {
        company_id_slug: { company_id, slug: process.slug }
      }
    });
    if (!templateProcess) {
      let data = {
        ...process,
        company_id,
        query_information: ""
      };
      if (process.query_information) {
        data.query_information = JSON.stringify(process.query_information);
      }
      templateProcess = await prisma.notificationTemplateProcess.create({
        data
      });
      console.log(
        `Whatsapp Template Process created successfully: ${templateProcess.name}`
      );
    } else {
      let data = {
        ...process,
        query_information: ""
      };
      if (process.query_information) {
        data.query_information = JSON.stringify(process.query_information);
      }
      templateProcess = await prisma.notificationTemplateProcess.update({
        where: { company_id_slug: { company_id, slug: process.slug } },
        data
      });
      console.log(
        `Whatsapp Template Process updated successfully: ${templateProcess.name}`
      );
    }
    return templateProcess;
  } catch (error) {
    console.log(`Whatsapp Template Process seeder failed: ${process.name}`);
    throw error;
  }
}
async function findOrCreateWhatsappRecipientTags(tag, company_id) {
  try {
    let templateTag = await prisma.notificationTemplateRecipientTag.findUnique({
      where: { company_id_slug_notification_type: { company_id, slug: tag.slug, notification_type: "whatsapp" } }
    });
    if (!templateTag) {
      let data = {
        ...tag,
        company_id,
        query_information: ""
      };
      if (tag.query_information) {
        data.query_information = JSON.stringify(tag.query_information);
      }
      templateTag = await prisma.notificationTemplateRecipientTag.create({
        data
      });
      console.log(
        `Whatsapp Template Recipient Tag created successfully: ${tag.slug}`
      );
    } else {
      let data = {
        ...tag,
        query_information: ""
      };
      if (tag.query_information) {
        data.query_information = JSON.stringify(tag.query_information);
      }
      templateTag = await prisma.notificationTemplateRecipientTag.update({
        where: { company_id_slug_notification_type: { company_id, slug: tag.slug, notification_type: "whatsapp" } },
        data
      });
      console.log(
        `Whatsapp Template Recipient Tag updated successfully: ${templateTag.slug}`
      );
    }
    return templateTag;
  } catch (error) {
    console.log(`Whatsapp Reciepient tag Process seeder failed: ${tag.slug}`);
    throw error;
  }
}
async function whatsappTemplatesProcessSeeder(company_id) {
  try {
    const company = await prisma.user.findFirst({
      where: { company_id }
    });
    if (!company) {
      console.log(
        `WARNING: whatsappTemplatesProcessSeeder: Company with id ${company_id} does not exist.`
      );
      return;
    }
    const datas = whatsappTemplateProcessSeeder;
    for (const process of datas) {
      await findOrCreateProcess(process, company_id);
    }
    const whatsappProcessAndTagGroup = [
      {
        whatsapp_template_process: "user-created",
        whatsapp_template_tag: "[user_firstname]"
      },
      {
        whatsapp_template_process: "user-created",
        whatsapp_template_tag: "[user_fullname]"
      },
      {
        whatsapp_template_process: "user-created",
        whatsapp_template_tag: "[user_email]"
      },
      {
        whatsapp_template_process: "user-created",
        whatsapp_template_tag: "[user_lastname]"
      },
      {
        whatsapp_template_process: "user-created",
        whatsapp_template_tag: "[username]"
      },
      {
        whatsapp_template_process: "user-created",
        whatsapp_template_tag: "[user_phone]"
      },
      {
        whatsapp_template_process: "user-created",
        whatsapp_template_tag: "[user_address]"
      },
      {
        whatsapp_template_process: "user-created",
        whatsapp_template_tag: "[user_roles]"
      }
    ];
    const recipientTags = whatsappRecipientsSeeder;
    for (const tag of recipientTags) {
      await findOrCreateWhatsappRecipientTags(tag, company_id);
    }
    console.log("Email Template process creation completed.");
  } catch (error) {
    throw error;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  whatsappTemplatesProcessSeeder
});
