// API endpoint for monitoring browser processes and cleanup status
import { NextResponse } from 'next/server'

export async function GET() {
  try {
    console.log('📊 System process monitoring request...')
    
    // Import cleanup service
    const { default: aggressiveCleanup } = await import('../../../../lib/aggressive-cleanup')
    const { progressTracker } = await import('../../../../lib/progress-tracker')
    
    // Get process information
    await aggressiveCleanup.getProcessInfo()
    
    // Get active sessions
    const activeSessions = progressTracker.getActiveSessions()
    const sessionDetails = activeSessions.map(sessionId => {
      const progress = progressTracker.getProgress(sessionId)
      return {
        sessionId,
        currentStep: progress?.currentStep || 0,
        totalSteps: progress?.totalSteps || 0,
        activeStep: progress?.steps.find(step => step.status === 'active')?.title || 'Unknown'
      }
    })
    
    return NextResponse.json({
      success: true,
      timestamp: Date.now(),
      activeSessions: sessionDetails,
      message: 'Process information logged to console'
    })
  } catch (error) {
    console.error('Error in process monitoring:', error)
    return NextResponse.json({
      success: false,
      error: error.message
    }, { status: 500 })
  }
}

export async function POST() {
  try {
    console.log('🧹 Manual aggressive cleanup triggered...')
    
    // Import cleanup service
    const { default: aggressiveCleanup } = await import('../../../../lib/aggressive-cleanup')
    
    // Run aggressive cleanup
    await aggressiveCleanup.runPostAnalysisCleanup()
    
    return NextResponse.json({
      success: true,
      message: 'Aggressive cleanup completed'
    })
  } catch (error) {
    console.error('Error in manual aggressive cleanup:', error)
    return NextResponse.json({
      success: false,
      error: error.message
    }, { status: 500 })
  }
}
