"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 userRoles_seeds_exports = {};
__export(userRoles_seeds_exports, {
  userRolesSeeds: () => userRolesSeeds
});
module.exports = __toCommonJS(userRoles_seeds_exports);
var import_client = require("@prisma/pg/client");
const prisma = new import_client.PrismaClient();
async function userRolesSeeds(companyId) {
  try {
    const userRoleMapping = {
      saravanak: ["Admin"]
    };
    const roles = await prisma.role.findMany({
      where: {
        company_id: companyId,
        name: { in: Object.values(userRoleMapping).flat() }
      },
      select: { id: true, name: true }
    });
    const roleNameToIdMap = roles.reduce(
      (map, role) => {
        map[role.name] = role.id;
        return map;
      },
      {}
    );
    let superAdminOrAdminUser = await prisma.user.findFirst({
      where: {
        company_id: companyId,
        OR: [{ role: "super_admin" }, { role: "admin" }]
      }
    });
    if (!superAdminOrAdminUser) {
      console.log(
        `User role seeder failed: Admin user not available for company id ${companyId}`
      );
      return false;
    }
    for (const [username, roleNames] of Object.entries(userRoleMapping)) {
      for (const roleName of roleNames) {
        const roleId = roleNameToIdMap[roleName];
        const user = await prisma.user.findUnique({
          where: {
            company_id_username: {
              company_id: companyId,
              username
            }
          }
        });
        if (!user) {
          console.error(
            `Invalid user name ${roleName} for user ID ${username}`
          );
          continue;
        }
        const userId = user.id;
        if (!roleId) {
          console.error(
            `Invalid role name ${roleName} for user ID ${username}`
          );
          continue;
        }
        const existingRole = await prisma.userRoles.findUnique({
          where: {
            company_id_user_id_role_id: {
              company_id: companyId,
              user_id: Number(userId),
              role_id: roleId
            }
          }
        });
        if (!existingRole) {
          await prisma.userRoles.create({
            data: {
              company_id: companyId,
              user_id: Number(userId),
              role_id: roleId
            }
          });
          console.log(
            `Created new role permission for company ${companyId}, Role ${roleId}, UserID ${userId}`
          );
        }
      }
    }
  } catch (error) {
    console.error(`Error in userRolesSeeds: ${error}`);
    if (error instanceof import_client.Prisma.PrismaClientKnownRequestError && error.code === "P2003" && error.meta) {
      console.error(`Foreign key constraint failed: ${error.meta.field_name}`);
    }
    throw error;
  } finally {
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  userRolesSeeds
});
