import { PrismaClient } from "./generated/prisma/client";
import { PrismaMssql } from "@prisma/adapter-mssql";
import { writeFileSync } from "fs";

const connectionString = process.env.DATABASE_URL;

if (!connectionString) {
  throw new Error("DATABASE_URL is not set.");
}

const adapter = new PrismaMssql(connectionString);
const prisma = new PrismaClient({ adapter });

const products = await prisma.productCatalog.findMany({
  where: { inactiveFlag: false },
  include: {
    subcategory: { include: { category: true } },
    manufacturer: true,
    inventory: true,
    itemVendors: true,
  },
  take: 100,
});

writeFileSync(
  "products-with-relations.json",
  JSON.stringify(products, null, 2),
);
console.log(`Exported ${products.length} products`);
await prisma.$disconnect();
