/**
 * Playwright Global Teardown
 * 
 * Runs once after all tests to clean up the test environment.
 */

import { FullConfig } from '@playwright/test';
import { cleanupTestData, getTestDataStats } from '../fixtures/database';

async function globalTeardown(config: FullConfig) {
  console.log('🧹 Starting global teardown...');

  try {
    // Get stats before cleanup
    const statsBefore = await getTestDataStats();
    console.log('📊 Test data before cleanup:', statsBefore);

    // Clean up all test data
    await cleanupTestData();

    // Verify cleanup
    const statsAfter = await getTestDataStats();
    console.log('📊 Test data after cleanup:', statsAfter);

    const totalRemaining = Object.values(statsAfter).reduce((sum, count) => sum + count, 0);
    if (totalRemaining > 0) {
      console.warn('⚠️  Some test data may not have been cleaned up properly');
    } else {
      console.log('✅ All test data cleaned up successfully');
    }

    console.log('✅ Global teardown complete');
  } catch (error) {
    console.error('❌ Global teardown failed:', error);
    // Don't throw - we don't want to fail the test run because of cleanup issues
  }
}

export default globalTeardown;
