import "dotenv/config";
import path from "node:path";
import { defineConfig } from "prisma/config";

function resolveSqlitePath() {
  const raw = process.env.DATABASE_URL?.trim();
  if (!raw) {
    return path.join(process.cwd(), "data", "vidoschool.db");
  }

  const withoutScheme = raw.replace(/^file:/i, "");
  if (path.isAbsolute(withoutScheme)) {
    return withoutScheme;
  }

  return path.resolve(process.cwd(), withoutScheme);
}

const dbPath = resolveSqlitePath();

export default defineConfig({
  schema: "prisma/schema.prisma",
  migrations: {
    path: "prisma/migrations",
    seed: "npx tsx prisma/seed.ts",
  },
  datasource: {
    url: `file:${dbPath}`,
  },
});
