"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 user_seeds_exports = {};
__export(user_seeds_exports, {
  usersSeeder: () => usersSeeder
});
module.exports = __toCommonJS(user_seeds_exports);
var import_client = require("@prisma/pg/client");
var import_uuid = require("uuid");
var import_bcryptjs = __toESM(require("bcryptjs"));
const prisma = new import_client.PrismaClient();
function getCurrentUtcTime() {
  return (/* @__PURE__ */ new Date()).toISOString();
}
const SUPER_ADMIN_SECRET = process.env.JWT_SECRET || "secret";
async function initPassword(plainPassword, role) {
  if (role === "super_admin") {
    const combinedPassword = `${plainPassword}:${SUPER_ADMIN_SECRET}`;
    const salt = await import_bcryptjs.default.genSalt(12);
    return await import_bcryptjs.default.hash(combinedPassword, salt);
  } else {
    return await import_bcryptjs.default.hash(plainPassword, 10);
  }
}
const sAdmins = [
  {
    email: "karthickn+1@techcedence.com",
    username: "sadmin",
    password: "SAdmin@123#",
    role: "super_admin",
    code: "1000",
    first_name: "Karthick",
    last_name: "SA"
  },
  {
    email: "praveenk+1@techcedence.com",
    username: "praveen",
    password: "Test@123",
    role: "company_admin",
    code: "1001",
    first_name: "Praveen",
    last_name: "CA"
  },
  {
    email: "saravanak+1@techcedence.com",
    username: "saravanak",
    password: "Test@123",
    role: "admin",
    code: "1004",
    first_name: "Saravana",
    last_name: "SA"
  }
];
async function usersSeeder(company_id) {
  try {
    const utcNow = getCurrentUtcTime();
    for (const admin of sAdmins) {
      let user = await prisma.user.findUnique({
        where: {
          company_id_username: { company_id, username: admin.username }
        }
      });
      if (user) {
        user = await prisma.user.update({
          where: { id: user.id },
          data: {
            email: admin.email,
            username: admin.username,
            password: await initPassword(admin.password, admin.role),
            role: admin.role,
            updated_by: user.id,
            updated_at: utcNow
          }
        });
        console.log(`User updated successfully: ${user.username}`);
      } else {
        user = await prisma.user.create({
          data: {
            uuid: (0, import_uuid.v4)(),
            email: admin.email,
            username: admin.username,
            password: await initPassword(admin.password, admin.role),
            role: admin.role,
            company_id,
            created_at: utcNow,
            updated_at: utcNow
          }
        });
        await prisma.userDetail.create({
          data: {
            user_id: user.id,
            code: admin.code,
            first_name: admin.first_name,
            last_name: admin.last_name,
            company_id,
            created_by: user.id,
            updated_by: user.id,
            created_at: utcNow,
            updated_at: utcNow
          }
        });
        console.log(`User created successfully: ${user.username}`);
      }
    }
  } catch (error) {
    console.log(`User seeder failed`);
    throw error;
  } finally {
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  usersSeeder
});
