"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 approval_workflow_seeds_exports = {};
__export(approval_workflow_seeds_exports, {
  approvalWorkflow: () => approvalWorkflow
});
module.exports = __toCommonJS(approval_workflow_seeds_exports);
var import_client = require("@prisma/pg/client");
const prisma = new import_client.PrismaClient();
function getCurrentUtcTime() {
  return (/* @__PURE__ */ new Date()).toISOString();
}
async function approvalWorkflow(company_id) {
  console.log("Seeding ApprovalWorkflow...");
  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 approvalWorkflows = [
    {
      slug: "name_starts_with_mukesh",
      name: "name starts with 'Mukesh'",
      description: "name starts with 'Mukesh'",
      query_information: `SELECT count(1) as exist FROM users u LEFT JOIN user_details ud ON ud.user_id = u.id WHERE u.email = $prefill_data.users.email AND u.status_id = 1 AND CONCAT($prefill_data.user_details.first_name, ' ', $prefill_data.user_details.last_name) ILIKE '%Mukesh%'`,
      company_id,
      created_by: createdBy,
      updated_by: createdBy,
      created_at: utcNow,
      updated_at: utcNow
    },
    {
      slug: "cosmetic_order_item_cat",
      name: "Cosmetic Order Item Category",
      description: "Cosmetic Order Item Category",
      query_information: `SELECT COUNT(1) as exist FROM orders as o 
                          INNER JOIN order_details as od ON od.order_id = o.id 
                          INNER JOIN product as p on p.id = od.item_id
                          WHERE o.id = $unique_id 
                          AND p.category_id = 5`,
      company_id,
      created_by: createdBy,
      updated_by: createdBy,
      created_at: utcNow,
      updated_at: utcNow
    },
    {
      slug: "high_value_transaction",
      name: "High Value Transaction Approval",
      description: "High Value Transaction Approval",
      query_information: `SELECT COUNT(1) as exist FROM transactions 
                          WHERE amount > 50000 
                          AND user_id = $session_user_id`,
      company_id,
      created_by: createdBy,
      updated_by: createdBy,
      created_at: utcNow,
      updated_at: utcNow
    },
    {
      slug: "finance_approval_required",
      name: "Finance Approval Required",
      description: "Finance Approval Required",
      query_information: `SELECT COUNT(1) as exist FROM orders 
                          WHERE id = $unique_id 
                          AND payment_status = 'pending' 
                          AND total_amount > 20000`,
      company_id,
      created_by: createdBy,
      updated_by: createdBy,
      created_at: utcNow,
      updated_at: utcNow
    }
  ];
  for (const workflow of approvalWorkflows) {
    await prisma.approvalWorkflow.upsert({
      where: { company_id_slug: { company_id, slug: workflow.slug } },
      update: workflow,
      create: workflow
    });
    await approvalWorkflowAssignment(
      company_id,
      workflow.slug,
      createdBy,
      utcNow
    );
  }
  console.log("ApprovalWorkflow seeding completed!");
}
async function approvalWorkflowAssignment(company_id, slug, created_by, utcNow) {
  console.log("Seeding ApprovalWorkflowAssignment...");
  const workFlow = await prisma.approvalWorkflow.findUnique({
    where: { company_id_slug: { company_id, slug } }
  });
  if (workFlow) {
    const approvalAssignments = [
      {
        approval_workflow_slug: workFlow.slug,
        approver_order_no: 1,
        approver_type: import_client.ApproverType.user_id,
        approver: "1",
        approve_mail_id: 9,
        reject_mail_id: 8,
        // approve_query_information: Prisma.JsonNull,
        // reject_query_information: Prisma.JsonNull,
        company_id,
        created_by,
        updated_by: created_by,
        created_at: utcNow,
        updated_at: utcNow
      },
      {
        approval_workflow_slug: workFlow.slug,
        approver_order_no: 2,
        approver_type: import_client.ApproverType.user_id,
        approver: "1,2",
        // approve_query_information: Prisma.JsonNull,
        // reject_query_information: Prisma.JsonNull,
        company_id,
        created_by,
        updated_by: created_by,
        created_at: utcNow,
        updated_at: utcNow
      },
      {
        approval_workflow_slug: workFlow.slug,
        approver_order_no: 3,
        approver_type: import_client.ApproverType.role_id,
        approver: "4",
        // approve_query_information: Prisma.JsonNull,
        // reject_query_information: Prisma.JsonNull,
        company_id,
        created_by,
        updated_by: created_by,
        created_at: utcNow,
        updated_at: utcNow
      }
    ];
    for (const assignment of approvalAssignments) {
      await prisma.approvalWorkflowAssignment.upsert({
        where: {
          company_id_approval_workflow_slug_approver_order_no: {
            company_id: assignment.company_id,
            approval_workflow_slug: assignment.approval_workflow_slug,
            approver_order_no: assignment.approver_order_no
          }
        },
        update: assignment,
        create: assignment
      });
    }
  }
  console.log("ApprovalWorkflowAssignment seeding completed!");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  approvalWorkflow
});
