/**
 * Global Setup for Playwright E2E Tests
 * 
 * This runs once before all test suites.
 * Used for:
 * - Database seeding
 * - Test environment preparation
 * - Creating test data
 */

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

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function globalSetup() {
  console.log('🚀 Starting E2E test setup...');

  // Ensure test data directories exist
  const testDataDir = path.join(__dirname, 'fixtures');
  if (!fs.existsSync(testDataDir)) {
    fs.mkdirSync(testDataDir, { recursive: true });
  }

  // Ensure screenshots directory exists
  const screenshotsDir = path.join(__dirname, 'screenshots');
  if (!fs.existsSync(screenshotsDir)) {
    fs.mkdirSync(screenshotsDir, { recursive: true });
  }

  // Ensure baseline directory exists for visual regression
  const baselineDir = path.join(screenshotsDir, 'baseline');
  if (!fs.existsSync(baselineDir)) {
    fs.mkdirSync(baselineDir, { recursive: true });
  }

  // Store test start time for cleanup tracking
  const testStartTime = new Date().toISOString();
  process.env.TEST_START_TIME = testStartTime;

  console.log('✅ E2E test setup complete');
  console.log(`   Test started at: ${testStartTime}`);
}

export default globalSetup;
