"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var email_template_assignment_seeds_exports = {};
__export(email_template_assignment_seeds_exports, {
  emailTemplateAssignmentsSeeder: () => emailTemplateAssignmentsSeeder
});
module.exports = __toCommonJS(email_template_assignment_seeds_exports);
var import_client = require("@prisma/pg/client");
const prisma = new import_client.PrismaClient();
const handleCcBcc = async (list, send_type, assignmentId, company_id, created_by) => {
  if (!list || list.length === 0 || !assignmentId) return;
  for (const item of list) {
    await prisma.emailTemplateCcBcc.upsert({
      where: {
        email_template_assignment_id_send_type_recipient_type_email_to: {
          email_template_assignment_id: assignmentId,
          send_type,
          recipient_type: item.recipient_type,
          email_to: item.email_to
        }
      },
      create: {
        email_template_assignment_id: assignmentId,
        send_type,
        recipient_type: item.recipient_type,
        email_to: item.email_to,
        company_id,
        created_by
      },
      update: {
        recipient_type: item.recipient_type,
        email_to: item.email_to
      }
    });
  }
};
async function emailTemplateAssignmentsSeeder(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 = [
      {
        notification_template_process_slug: "user_created",
        template_slug: "user_template_user_created",
        recipient_type: "tag",
        notification_to: "requested_user",
        notification_type: import_client.NotificationType.email
      },
      {
        notification_template_process_slug: "user_created",
        template_slug: "admin_template_user_created",
        recipient_type: "tag",
        notification_to: "super_admin",
        notification_type: import_client.NotificationType.email
      },
      {
        notification_template_process_slug: "user_created",
        template_slug: "department_head_template_user_created",
        recipient_type: "tag",
        notification_to: "department_head",
        notification_type: import_client.NotificationType.email
      },
      {
        notification_template_process_slug: "forgot_password",
        template_slug: "forgot_password",
        recipient_type: "tag",
        notification_to: "requested_user",
        notification_type: import_client.NotificationType.email
      },
      {
        notification_template_process_slug: "login_otp",
        template_slug: "login_otp",
        recipient_type: "tag",
        notification_to: "requested_user",
        notification_type: import_client.NotificationType.email
      },
      {
        notification_template_process_slug: "notification_verify",
        template_slug: "notification_verify",
        recipient_type: "tag",
        notification_to: "requested_user",
        notification_type: import_client.NotificationType.email
      },
      {
        notification_template_process_slug: "mail_import",
        template_slug: "mail_import",
        recipient_type: "tag",
        notification_to: "requested_user",
        notification_type: import_client.NotificationType.email
      },
      {
        notification_template_process_slug: "approval_user_mail",
        template_slug: "user_accepted",
        recipient_type: "tag",
        notification_to: "requested_user",
        notification_type: import_client.NotificationType.email
      },
      {
        notification_template_process_slug: "reject_user_mail",
        template_slug: "user_rejected",
        recipient_type: "tag",
        notification_to: "requested_user",
        notification_type: import_client.NotificationType.email
      },
      {
        notification_template_process_slug: "email_verify",
        template_slug: "email_verify",
        recipient_type: "tag",
        notification_to: "requested_user",
        notification_type: import_client.NotificationType.email
      }
    ];
    for (const data of datas) {
      const emailTemplateProcess = await prisma.notificationTemplateProcess.findFirst({
        where: {
          slug: data.notification_template_process_slug,
          company_id,
          notification_type: data.notification_type
        }
      });
      if (!emailTemplateProcess) {
        console.error(
          `Email template process with slug "${data.notification_template_process_slug}" not found for company_id ${company_id}.`
        );
        continue;
      }
      let template_id = null;
      if (data.template_slug) {
        const template = await prisma.notificationTemplate.findFirst({
          where: {
            slug: data.template_slug,
            company_id,
            notification_type: data.notification_type
          }
        });
        if (template) {
          template_id = template.id;
        } else {
          console.error(
            `User template with slug "${data.template_slug}" 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,
              notification_type: data.notification_type
            }
          });
          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 email_template_process_id = emailTemplateProcess.id;
      let existingAssignment = await prisma.notificationTemplateAssignment.findFirst({
        where: {
          notification_template_process_id: email_template_process_id,
          template_id: template_id || 0,
          recipient_type: data.recipient_type,
          notification_to,
          company_id,
          notification_type: data.notification_type
        }
      });
      if (!existingAssignment && template_id) {
        existingAssignment = await prisma.notificationTemplateAssignment.create(
          {
            data: {
              notification_template_process_id: email_template_process_id,
              template_id,
              recipient_type: data.recipient_type,
              notification_to,
              company_id,
              created_by,
              created_at: /* @__PURE__ */ new Date(),
              notification_type: data?.notification_type || import_client.NotificationType.email
            }
          }
        );
        console.log(
          `Email Template Assignment with email_template_process_id "${email_template_process_id}" created for company_id ${company_id}.`
        );
      } else {
        console.log(
          `Email Template Assignment with email_template_process_id "${email_template_process_id}" already exists for company_id ${company_id}.`
        );
      }
      await Promise.all([
        handleCcBcc(
          data.cc,
          "cc",
          existingAssignment?.id ?? null,
          company_id,
          created_by
        ),
        handleCcBcc(
          data.bcc,
          "bcc",
          existingAssignment?.id ?? null,
          company_id,
          created_by
        )
      ]);
    }
    console.log("Email Template Assignment creation completed.");
  } catch (error) {
    console.error("Error creating email template assignments:", error);
    throw error;
  } finally {
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  emailTemplateAssignmentsSeeder
});
