/**
 * Test Environment Setup
 * Load test environment variables before tests run
 */

import { execSync } from 'child_process';
import path from 'path';
import fs from 'fs';

// Use development database for tests (simplest approach)
process.env.DATABASE_URL = 'file:./dev.db';
process.env.JWT_SECRET = 'test-secret-key-for-jest-development-purpose';
process.env.NODE_ENV = 'test';

// Initialize test database if needed
const dbPath = path.join(__dirname, '..', 'dev.db');
if (!fs.existsSync(dbPath)) {
  try {
    const schemaPath = path.join(__dirname, '..', 'prisma', 'schema.prisma');
    execSync(`npx prisma db push --schema="${schemaPath}" --skip-generate`, {
      stdio: 'inherit',
      cwd: path.join(__dirname, '..'),
      windowsHide: true
    });
    console.log('✅ Test database initialized');
  } catch (error) {
    console.error('❌ Failed to initialize test database:', error);
    throw error;
  }
} else {
  console.log('✅ Using existing database');
}
