"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 theme_seeds_exports = {};
__export(theme_seeds_exports, {
  themeAndLineItemsSeeder: () => themeAndLineItemsSeeder
});
module.exports = __toCommonJS(theme_seeds_exports);
var import_client = require("@prisma/pg/client");
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
const prisma = new import_client.PrismaClient();
function readThemes(jsonPath) {
  const raw = import_fs.default.readFileSync(jsonPath, "utf-8");
  const parsed = JSON.parse(raw);
  if (Array.isArray(parsed)) return parsed;
  if (Array.isArray(parsed?.themes)) return parsed.themes;
  throw new Error(`Invalid theme JSON at ${jsonPath}. Expected an array or { "themes": [...] }`);
}
async function themeAndLineItemsSeeder(company_id, jsonPath = import_path.default.join(__dirname, "json", "theme-seeds.json")) {
  const adminUser = await prisma.user.findFirst({
    where: { company_id, role: "super_admin" },
    select: { id: true }
  });
  if (!adminUser) {
    console.log(`Theme seeder failed: super_admin not found for company_id ${company_id}`);
    return false;
  }
  const themes = readThemes(jsonPath);
  for (const raw of themes) {
    const lineItems = raw.themeLineItems ?? raw.line_items ?? [];
    await prisma.$transaction(async (tx) => {
      let theme = await tx.themes.findFirst({
        where: { company_id, name: raw.name },
        orderBy: { id: "asc" },
        // pick a canonical row if multiples exist
        select: { id: true, name: true }
      });
      if (!theme) {
        theme = await tx.themes.create({
          data: {
            company_id,
            name: raw.name,
            description: raw.description ?? null,
            reference_images: raw.reference_images ?? null,
            status_id: raw.status_id ?? 1,
            created_by: adminUser.id,
            updated_by: adminUser.id
            // created_at/updated_at handled by defaults/@updatedAt
          },
          select: { id: true, name: true }
        });
        console.log(`Theme created: ${theme.name}`);
      } else {
        await tx.themes.update({
          where: { id: theme.id },
          data: {
            description: raw.description ?? null,
            reference_images: raw.reference_images ?? null,
            status_id: raw.status_id ?? 1,
            updated_by: adminUser.id
          }
        });
        console.log(`Theme updated: ${theme.name}`);
      }
      for (const li of lineItems) {
        const existingLI = await tx.themeLineItems.findFirst({
          where: {
            company_id,
            theme_id: theme.id,
            attribute: li.attribute
          },
          orderBy: { id: "asc" },
          // canonical row if duplicates exist
          select: { id: true }
        });
        if (!existingLI) {
          await tx.themeLineItems.create({
            data: {
              company_id,
              theme_id: theme.id,
              attribute: li.attribute,
              value: li.value,
              description: li.description ?? null,
              status_id: li.status_id ?? 1,
              created_by: adminUser.id,
              updated_by: adminUser.id
              // timestamps auto
            }
          });
          console.log(`  \u2022 LineItem created: ${li.attribute}`);
        } else {
          await tx.themeLineItems.update({
            where: { id: existingLI.id },
            data: {
              value: li.value,
              description: li.description ?? null,
              status_id: li.status_id ?? 1,
              updated_by: adminUser.id
            }
          });
          console.log(`  \u2022 LineItem updated: ${li.attribute}`);
        }
      }
    });
  }
  console.log("Theme + Line Items seeding completed.");
  return true;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  themeAndLineItemsSeeder
});
