import { NextRequest } from 'next/server';
import { progressStore, type ProgressUpdate } from '../../../../lib/progress';

// Configure route timeout for long-running progress streams
export const maxDuration = 900; // 15 minutes (900 seconds)

export async function GET(request: NextRequest) {
  const searchParams = request.nextUrl.searchParams;
  const sessionId = searchParams.get('sessionId');
  
  if (!sessionId) {
    return new Response('Session ID required', { status: 400 });
  }

  // Set up Server-Sent Events
  const stream = new ReadableStream({
    start(controller) {
      const encoder = new TextEncoder();
      
      // Send initial connection message
      const data = `data: ${JSON.stringify({ type: 'connected', sessionId })}\n\n`;
      controller.enqueue(encoder.encode(data));

      // Check for progress updates every 500ms
      const interval = setInterval(() => {
        const updates = progressStore.get(sessionId);
        if (updates && updates.length > 0) {
          // Send all pending updates
          updates.forEach(update => {
            const data = `data: ${JSON.stringify(update)}\n\n`;
            controller.enqueue(encoder.encode(data));
          });
          
          // Clear sent updates
          progressStore.delete(sessionId);
          
          // If this was a completion or error, close the stream
          const lastUpdate = updates[updates.length - 1];
          if (lastUpdate.type === 'completed' || lastUpdate.type === 'error') {
            clearInterval(interval);
            controller.close();
          }
        }
      }, 500);

      // Cleanup after timeout
      setTimeout(() => {
        clearInterval(interval);
        progressStore.delete(sessionId);
        controller.close();
      }, 900000); // 15 minutes
    }
  });

  return new Response(stream, {
    headers: {
      'Content-Type': 'text/event-stream',
      'Cache-Control': 'no-cache',
      'Connection': 'keep-alive',
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Headers': 'Cache-Control',
    },
  });
}

