import dotenv from "dotenv"
import path from "node:path"

import { defineConfig } from "prisma/config"

// Carrega .env e .env.local (local tem prioridade)
dotenv.config({ path: ".env" })
dotenv.config({ path: ".env.local", override: true })

function getDatabaseUrl() {
  const url = process.env.DATABASE_URL?.trim()

  if (!url) {
    throw new Error(
      "DATABASE_URL is required. Set it to a PostgreSQL connection string."
    )
  }

  return url
}

export default defineConfig({
  schema: path.join("prisma", "schema.prisma"),
  migrations: {
    path: path.join("prisma", "migrations"),
  },
  datasource: {
    url: getDatabaseUrl(),
  },
})
