import { mkdirSync, rmSync } from 'fs';
import { join, resolve } from 'path';
import Database from 'bun:sqlite';
import { initializeDatabase } from '../../../src/db';

export function createTempDb(): { path: string; db: any; cleanup: () => void } {
  const dir = join('tmp', 'web-tests');
  try { mkdirSync(dir, { recursive: true }); } catch {}
  const path = join(dir, `db-${Date.now()}-${Math.random().toString(16).slice(2)}.sqlite`);
  const absPath = resolve(path);
  const db = new Database(absPath);
  initializeDatabase(db);
  const cleanup = () => {
    try { db.close(); } catch {}
    try { rmSync(absPath); } catch {}
  };
  return { path: absPath, db, cleanup };
}
