/**
 * Test helpers for repository tests.
 *
 * Provides utilities for setting up in-memory test databases
 * with schema applied.
 */

import Database from 'better-sqlite3';
import { drizzle } from 'drizzle-orm/better-sqlite3';
import type { DrizzleDatabase } from '../../index.js';
import { ensureSchema } from '../../ensure-schema.js';
import * as schema from '../../schema.js';

/**
 * Create an in-memory test database with schema applied.
 * Returns a fresh Drizzle instance for each call.
 */
export function createTestDatabase(): DrizzleDatabase {
  const sqlite = new Database(':memory:');

  // Enable foreign keys
  sqlite.pragma('foreign_keys = ON');

  const db = drizzle(sqlite, { schema });

  // Create all tables
  ensureSchema(db);

  return db;
}
