export const dynamic = 'force-dynamic';

import type { NextRequest } from "next/server"

// Store active connections
const connections = new Set<ReadableStreamDefaultController>()

export async function GET(request: NextRequest) {
  const stream = new ReadableStream({
    start(controller) {
      connections.add(controller)

      // Send initial connection message
      controller.enqueue(`data: ${JSON.stringify({ type: "connected", timestamp: new Date().toISOString() })}\n\n`)

      // Keep connection alive with periodic pings
      const pingInterval = setInterval(() => {
        try {
          controller.enqueue(`data: ${JSON.stringify({ type: "ping", timestamp: new Date().toISOString() })}\n\n`)
        } catch (err) {
          clearInterval(pingInterval)
          connections.delete(controller)
        }
      }, 30000)

      // Clean up on close
      request.signal.addEventListener("abort", () => {
        clearInterval(pingInterval)
        connections.delete(controller)
        try {
          controller.close()
        } catch (err) {
          // Connection already closed
        }
      })
    },
  })

  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",
    },
  })
}

// Function to broadcast updates to all connected clients
export function broadcastUpdate(data: any) {
  const message = `data: ${JSON.stringify({ type: "update", data, timestamp: new Date().toISOString() })}\n\n`

  connections.forEach((controller) => {
    try {
      controller.enqueue(message)
    } catch (err) {
      connections.delete(controller)
    }
  })
}
