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

const databaseUrl = Bun.env.DATABASE_URL;

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

const adapter = new PrismaMssql(databaseUrl, { schema: "dbo" });

export const prisma = new PrismaClient({ adapter });

// Global error handler for unhandled Prisma promise rejections
process.on("unhandledRejection", (reason, promise) => {
  if (reason && typeof reason === "object" && "code" in reason) {
    console.error("Unhandled Prisma Rejection:", reason);
  }
});
