"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/sql/client");
const prisma = new import_client.PrismaClient();
async function userRolesSeeds(companyId) {
  try {
    console.log("\u{1F680} Starting User Roles Seeder...");
    const userRoleMapping = {
      "mukesh": ["Manager"],
      "raj": ["Supervisor"],
      "mukeshmtechcedence": ["Support Agent", "Supervisor"]
    };
    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;
    }, {});
    const superAdminOrAdminUser = await prisma.user.findFirst({
      where: {
        company_id: companyId,
        OR: [{ role: "super_admin" }, { role: "admin" }]
      }
    });
    if (!superAdminOrAdminUser) {
      console.log(`\u274C User role seeder failed: No admin user found for company_id ${companyId}`);
      return false;
    }
    for (const [username, roleNames] of Object.entries(userRoleMapping)) {
      const user = await prisma.user.findFirst({
        where: {
          company_id: companyId,
          username
        }
      });
      if (!user) {
        console.error(`\u274C Skipping: User ${username} does not exist for company_id ${companyId}`);
        continue;
      }
      for (const roleName of roleNames) {
        const roleId = roleNameToIdMap[roleName];
        if (!roleId) {
          console.error(`\u274C Skipping: Invalid role name ${roleName} for user ${username}`);
          continue;
        }
        const existingRole = await prisma.userRoles.findFirst({
          where: {
            company_id: companyId,
            user_id: user.id,
            role_id: roleId
          }
        });
        if (!existingRole) {
          await prisma.userRoles.create({
            data: {
              company_id: companyId,
              user_id: user.id,
              role_id: roleId
            }
          });
          console.log(`\u2705 Created role assignment: Company ${companyId}, Role ${roleName}, User ${username}`);
        } else {
          console.log(`\u{1F504} Role assignment already exists: Company ${companyId}, Role ${roleName}, User ${username}`);
        }
      }
    }
    console.log("\u2705 User Roles Seeder completed successfully.");
  } catch (error) {
    console.error(`\u274C 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 {
    await prisma.$disconnect();
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  userRolesSeeds
});
