"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/sql/client");
const prisma = new import_client.PrismaClient();
async function emailTemplateAssignmentsSeeder(company_id) {
  try {
    console.log("\u{1F680} Starting Email Template Assignments Seeder...");
    const user = await prisma.user.findFirst({
      where: { company_id, role: "super_admin" }
    });
    if (!user) {
      console.log(`\u274C Email Templates Assignment Seeder failed: No super_admin user found for company_id ${company_id}`);
      return false;
    }
    const created_by = user.id;
    const datas = [
      {
        "email_template_process_slug": "user-created",
        "template_slug": "user_template_user_created",
        "recipient_type": "tag",
        "email_to": "requested_user"
      },
      {
        "email_template_process_slug": "user-created",
        "template_slug": "admin_template_user_created",
        "recipient_type": "tag",
        "email_to": "super_admin"
      },
      {
        "email_template_process_slug": "user-created",
        "template_slug": "department_head_template_user_created",
        "recipient_type": "tag",
        "email_to": "department_head"
      },
      {
        "email_template_process_slug": "forgot-password",
        "template_slug": "forgot-password",
        "recipient_type": "tag",
        "email_to": "requested_user"
      },
      {
        "email_template_process_slug": "login-otp",
        "template_slug": "login-otp",
        "recipient_type": "tag",
        "email_to": "requested_user"
      },
      {
        "email_template_process_slug": "email-verify",
        "template_slug": "email-verify",
        "recipient_type": "tag",
        "email_to": "requested_user"
      }
    ];
    const assignmentsToCreate = [];
    for (const data of datas) {
      const emailTemplateProcess = await prisma.emailTemplateProcess.findFirst({
        where: { slug: data.email_template_process_slug, company_id }
      });
      if (!emailTemplateProcess) {
        console.log(`\u26A0\uFE0F Skipping: Email Template Process with slug "${data.email_template_process_slug}" not found for company_id ${company_id}.`);
        continue;
      }
      let template_id = null;
      if (data.template_slug) {
        const template = await prisma.emailTemplate.findFirst({
          where: { slug: data.template_slug, company_id }
        });
        if (template) {
          template_id = template.id;
        } else {
          console.error(`\u274C Skipping: Template with slug "${data.template_slug}" not found for company_id ${company_id}.`);
          continue;
        }
      }
      let email_to = data.email_to || null;
      if (data.recipient_type === "tag" && data.email_to) {
        const template = await prisma.emailTemplateRecipientTag.findFirst({
          where: { slug: data.email_to, company_id }
        });
        if (template) {
          email_to = `${template.id}`;
        } else {
          console.error(`\u274C Skipping: Email template recipient tag with slug "${data.email_to}" not found for company_id ${company_id}.`);
          continue;
        }
      }
      const email_template_process_id = emailTemplateProcess.id;
      const existingAssignment = await prisma.emailTemplateAssignment.findFirst({
        where: {
          email_template_process_id,
          template_id: template_id || 0,
          recipient_type: data.recipient_type,
          email_to,
          company_id
        }
      });
      if (!existingAssignment && template_id) {
        assignmentsToCreate.push(
          prisma.emailTemplateAssignment.create({
            data: {
              email_template_process_id,
              template_id,
              recipient_type: data.recipient_type,
              email_to,
              company_id,
              created_by,
              created_at: /* @__PURE__ */ new Date()
            }
          }).then(() => {
            console.log(`\u2705 Created Email Template Assignment for company_id ${company_id}.`);
          }).catch((error) => {
            console.error(`\u274C Error creating email template assignment:`, error);
          })
        );
      } else {
        console.log(`\u{1F504} Email Template Assignment already exists for company_id ${company_id}.`);
      }
    }
    await Promise.all(assignmentsToCreate);
    console.log("\u2705 Email Templates Assignment Seeder completed successfully.");
  } catch (error) {
    console.error(`\u274C Error in emailTemplateAssignmentsSeeder: ${error}`);
    throw error;
  } finally {
    await prisma.$disconnect();
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  emailTemplateAssignmentsSeeder
});
