import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";

// During Next.js build (NEXT_PHASE=phase-production-build), route handlers are
// statically analyzed but never executed — skip the DB check to allow the build
// to complete without a live DATABASE_URL.
const isBuildPhase = process.env.NEXT_PHASE === "phase-production-build";

if (!isBuildPhase && !process.env.DATABASE_URL) {
  throw new Error("DATABASE_URL env var is required");
}

const client = postgres(process.env.DATABASE_URL ?? "postgres://build-placeholder");

export const db = drizzle(client);
