import "server-only";
import path from "node:path";

/**
 * Resolve the on-disk SQLite path from DATABASE_URL or the default
 * `data/vidoschool.db` under the application root.
 *
 * Supported DATABASE_URL forms:
 * - file:./data/vidoschool.db
 * - file:data/vidoschool.db
 * - file:/home/user/vidoschool-data/vidoschool.db
 */
export function resolveSqlitePath() {
  const raw = process.env.DATABASE_URL?.trim();
  if (!raw) {
    return path.join(/*turbopackIgnore: true*/ process.cwd(), "data", "vidoschool.db");
  }

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

  return path.resolve(/*turbopackIgnore: true*/ process.cwd(), withoutScheme);
}
