/**
 * Global Setup - Runs ONCE before all test suites
 * Initialize test server and database here
 */

const Server = require('../src/server');

module.exports = async () => {
  console.log('\n🔧 Global Test Setup - Initializing test server...\n');
  
  // Set test environment variables
  process.env.NODE_ENV = 'test';
  process.env.PORT = 5001;
  process.env.ADMIN_SESSION_SECRET = process.env.ADMIN_SESSION_SECRET || 'test-session-secret';

  try {
    // Create and initialize server
    console.log('Creating server instance...');
    const serverInstance = new Server(5001);
    
    console.log('Initializing app...');
    const app = await serverInstance.initializeApp();

    // Store in global scope for all tests
    global.__TEST_SERVER__ = serverInstance;
    global.__TEST_APP__ = app;

    console.log('✅ Test server initialized successfully\n');
  } catch (error) {
    console.error('❌ Failed to initialize test server:', error);
    throw error;
  }
};
