import { execSync } from 'child_process';
import * as path from 'path';

export default async function globalTeardown() {
  console.log('\n🧹 Cleaning up E2E test environment...');

  const useDocker = process.env.USE_DOCKER !== 'false';

  if (useDocker) {
    try {
      // Check if Docker is available
      execSync('docker info', { stdio: 'ignore' });

      // Stop and remove test database container
      console.log('📦 Stopping test database container...');
      execSync('docker-compose -f docker-compose.test.yml down -v', {
        cwd: path.resolve(__dirname, '../..'),
        stdio: 'inherit',
      });

      console.log('✅ E2E test environment cleanup complete!\n');
    } catch (error) {
      console.log('⚠️  Docker not available or container already stopped');
    }
  } else {
    console.log('📌 Docker disabled. Skipping container cleanup...');
    console.log('✅ E2E test environment cleanup complete!\n');
  }
}
