"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 department_seeds_exports = {};
__export(department_seeds_exports, {
  departmentSeeds: () => departmentSeeds
});
module.exports = __toCommonJS(department_seeds_exports);
var import_client = require("@prisma/pg/client");
const prisma = new import_client.PrismaClient();
function getCurrentUtcTime() {
  return (/* @__PURE__ */ new Date()).toISOString();
}
async function departmentSeeds(company_id) {
  try {
    let user = await prisma.user.findFirst({
      where: { company_id, role: "super_admin" }
    });
    if (!user) {
      console.log(
        `Department seeder failed: admin user not available for company id ${company_id}`
      );
      return false;
    }
    const createdBy = user.id;
    const utcNow = getCurrentUtcTime();
    const departments = [
      {
        code: "ece",
        name: "ECE",
        status_id: 1,
        department_head_id: 1,
        company_id,
        created_by: 3,
        updated_by: createdBy,
        created_at: utcNow,
        updated_at: utcNow
      },
      {
        code: "eee",
        name: "EEE",
        status_id: 1,
        department_head_id: 1,
        company_id,
        created_by: createdBy,
        updated_by: createdBy,
        created_at: utcNow,
        updated_at: utcNow
      },
      {
        code: "fashion",
        name: "Fashion Technology",
        status_id: 1,
        department_head_id: 1,
        company_id,
        created_by: createdBy,
        updated_by: createdBy,
        created_at: utcNow,
        updated_at: utcNow
      }
    ];
    const codes = departments.map((dept) => dept.code);
    const existingDepartments = await prisma.department.findMany({
      where: { company_id, code: { in: codes } }
    });
    const existingDepartmentNames = new Set(
      existingDepartments.map((dept) => dept)
    );
    const deptPromises = [];
    for (let dept of departments) {
      if (!existingDepartmentNames.has(dept.code)) {
        deptPromises.push(
          prisma.department.upsert({
            create: dept,
            update: { ...dept, updated_by: createdBy },
            where: {
              company_id_code: { code: dept.code, company_id }
            }
          }).then((dept2) => {
            console.log(
              `Department ${dept2.name} created successfully. id is ${dept2.id}`
            );
          }).catch((error) => {
            console.error(
              `Failed to create department ${dept.name} for user_id ${createdBy}:`,
              error
            );
          })
        );
      } else {
        console.log(
          `Department ${dept.name} already exists for company_id ${company_id}, skipping creation.`
        );
      }
    }
    await Promise.all(deptPromises);
    console.log("Department seeding completed.");
  } catch (error) {
    console.error("Department seeding failed:", error);
  } finally {
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  departmentSeeds
});
