/**
 * Database testing helpers
 */

import { Pool } from 'pg';

/**
 * Seed database with test data
 */
export async function seedDatabase(pool: Pool): Promise<void> {
  // Create test tables if they don't exist
  await pool.query(`
    CREATE TABLE IF NOT EXISTS test_users (
      id UUID PRIMARY KEY,
      email VARCHAR(255) UNIQUE NOT NULL,
      name VARCHAR(255) NOT NULL,
      created_at TIMESTAMP DEFAULT NOW(),
      updated_at TIMESTAMP DEFAULT NOW()
    );
  `);

  await pool.query(`
    CREATE TABLE IF NOT EXISTS test_documents (
      id UUID PRIMARY KEY,
      title VARCHAR(255) NOT NULL,
      type VARCHAR(50) NOT NULL,
      content TEXT,
      file_url VARCHAR(500),
      created_at TIMESTAMP DEFAULT NOW(),
      updated_at TIMESTAMP DEFAULT NOW()
    );
  `);
}

/**
 * Clean up test data
 */
export async function cleanupDatabase(pool: Pool): Promise<void> {
  await pool.query('TRUNCATE TABLE test_users, test_documents CASCADE');
}

/**
 * Create a test database connection
 */
export function createTestPool(connectionString?: string): Pool {
  return new Pool({
    connectionString: connectionString || process.env.TEST_DATABASE_URL || 'postgresql://test:test@localhost:5432/test',
    max: 5,
  });
}

