import Database from 'better-sqlite3';
import path from 'node:path';
import fs from 'node:fs';

const defaultPath = path.join(process.cwd(), 'data', 'auth.db');
const dbPath = process.env.DATABASE_URL?.startsWith('file:')
  ? process.env.DATABASE_URL.replace(/^file:/, '')
  : process.env.DATABASE_PATH || defaultPath;

const dir = path.dirname(dbPath);
if (!fs.existsSync(dir)) {
  fs.mkdirSync(dir, { recursive: true });
}

export const db = new Database(dbPath);
