"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_seeds_exports = {};
__export(email_template_seeds_exports, {
  emailTemplatesSeeder: () => emailTemplatesSeeder
});
module.exports = __toCommonJS(email_template_seeds_exports);
var import_client = require("@prisma/pg/client");
var import_uuid = require("uuid");
const prisma = new import_client.PrismaClient();
const datas = [
  {
    slug: "forgot_password",
    name: "Forgot Password",
    subject: "Account Password Reset Request",
    email_body: "<p>Please use the following OTP to reset your password:</p><h2>[otp]</h2> <p>If you didn't request this OTP, please ignore this email.</p>",
    notification_template_process_slug: "forgot_password",
    notification_type: import_client.NotificationType.email,
    status_id: 1
  },
  {
    slug: "login_otp",
    name: "Login OTP",
    subject: "Your One-Time Password (OTP) for Login Verification",
    email_body: "<p>Please use this OTP to login to your account:</p> <h2>[otp]</h2> <p>If you didn't request this OTP, please ignore this email.</p>",
    notification_template_process_slug: "login_otp",
    notification_type: import_client.NotificationType.email,
    status_id: 1
  },
  {
    slug: "email_verify",
    name: "Email Verification",
    subject: "Verify Email Address",
    email_body: "<p>Please use the following OTP to verify your email address:</p> <h2>[otp]</h2> <p>If you didn't request this OTP, please ignore this email.</p>",
    notification_template_process_slug: "email_verify",
    notification_type: import_client.NotificationType.email,
    status_id: 1
  },
  {
    slug: "user_register",
    name: "User Register",
    subject: "New User Registration",
    email_body: "<h1>Welcome aboard!</h1><p>Thank you for registering with us.</p>",
    notification_template_process_slug: "user_register",
    notification_type: import_client.NotificationType.email,
    status_id: 1
  },
  {
    slug: "user_template_user_created",
    name: "user template - user created",
    subject: "Welcome to LCP",
    email_body: '<p>Hi [user_fullname], </p><p></p><h1 class="ql-align-center">Welcome aboard!</h1><p class="ql-align-center">Thank you for registering with us.</p><p class="ql-align-center">Your user details given below.</p><p class="ql-align-center"></p><ol><li>email : [user_email]</li><li>phone : [user_phone]</li><li>username : [username]</li></ol>',
    notification_template_process_slug: "user_created",
    notification_type: import_client.NotificationType.email,
    status_id: 1
  },
  {
    slug: "admin_template_user_created",
    name: "admin template - user created",
    subject: "New user created",
    email_body: '<p>Hi Admin,</p><p></p><h1 class="ql-align-center">New user created!</h1><p class="ql-align-center">User details given below.</p><p class="ql-align-center"></p><ol><li>email : [user_email]</li><li>phone : [user_phone]</li><li>username : [username]</li></ol>',
    notification_template_process_slug: "user_created",
    notification_type: import_client.NotificationType.email,
    status_id: 1
  },
  {
    slug: "department_head_template_user_created",
    name: "department head template - user created",
    subject: "New user created in your Department",
    email_body: '<p>Hi,</p><p></p><h1 class="ql-align-center">New user created in your Department!</h1><p class="ql-align-center">User details given below.</p><p class="ql-align-center"></p><ol><li>email : [user_email]</li><li>phone : [user_phone]</li><li>username : [username]</li></ol>',
    notification_template_process_slug: "user_created",
    notification_type: import_client.NotificationType.email,
    status_id: 1
  },
  {
    slug: "mail_import",
    name: "Mail Import",
    subject: "Details Imported by  [user_fullname],",
    email_body: "<p>Hi, [user_email] - [user_fullname] data has been imported and attached the details in the below file</p><p></p>",
    notification_template_process_slug: "mail_import",
    notification_type: import_client.NotificationType.email,
    status_id: 1
  },
  {
    slug: "user_rejected",
    name: "reject_user_mail",
    subject: "Request Rejected",
    email_body: "<p>Hi [user_fullname],</p><p></p><p>The latest update on your profile : [user_email], has been rejected by the admin.</p><p></p>",
    notification_template_process_slug: "reject_user_mail",
    notification_type: import_client.NotificationType.email,
    status_id: 1
  },
  {
    slug: "user_accepted",
    name: "accept_user_mail",
    subject: "Request Accepted",
    email_body: "<p>Hi [user_fullname],</p><p></p><p>The latest update on your profile : [user_email], has been accepted by the admin.</p>",
    notification_template_process_slug: "approval_user_mail",
    notification_type: import_client.NotificationType.email,
    status_id: 1
  }
];
async function emailTemplatesSeeder(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;
    }
    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.log(
          `Email Template Process with slug "${data.notification_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,
          notification_type: data.notification_type
        }
      });
      if (!existingTemplate) {
        try {
          await prisma.notificationTemplate.create({
            data: {
              uuid: (0, import_uuid.v4)(),
              slug: data.slug,
              name: data.name,
              email_subject: data.subject,
              email_body: data.email_body,
              notification_template_process_id: emailTemplateProcess.id,
              notification_type: data.notification_type,
              status_id: data.status_id,
              company_id,
              created_by
            }
          });
          console.log(
            `Created new Email Template: ${data.name} for company_id ${company_id}`
          );
        } catch (createError) {
          console.error(
            `Error creating email template with slug "${data.slug}":`,
            createError
          );
        }
      } else {
        await prisma.notificationTemplate.update({
          where: {
            company_id_slug_notification_type: {
              company_id,
              slug: data.slug,
              notification_type: data.notification_type
            }
          },
          data: {
            slug: data.slug,
            name: data.name,
            email_subject: data.subject,
            email_body: data.email_body,
            notification_template_process_id: emailTemplateProcess.id,
            notification_type: data.notification_type,
            status_id: data.status_id,
            updated_by: created_by
          }
        });
        console.log(
          `Email Template with slug "${data.slug}" already exists for company_id ${company_id}.`
        );
      }
    }
  } catch (error) {
    console.error(`Error in seedEmailTemplates: ${error}`);
    throw error;
  } finally {
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  emailTemplatesSeeder
});
