import { TEST_CONFIG } from 'playwright.config'

export default async function globalTeardown() {
  if (!TEST_CONFIG.enableBackendShutdown) {
    console.log('Backend shutdown disabled')
    return
  }
  // Use the shutdown endpoint to stop the backend
  if (!TEST_CONFIG.apiURL) {
    throw new Error('API_URL environment variable not set')
  }
  try {
    const response = await fetch(`${TEST_CONFIG.apiURL}/shutdown`, { method: 'POST' })
    if (!response.ok) {
      throw new Error(`Backend shutdown failed: ${response.statusText}`)
    }
    console.log('Backend shutdown successfully')
  } catch (error) {
    console.error('Error shutting down backend:', error)
  }
}
