import { NextResponse } from "next/server";

const EVENT_BUS_URL = process.env.EVENT_BUS_URL || "http://127.0.0.1:8089";

export const dynamic = "force-dynamic";

export async function GET() {
  try {
    const upstream = await fetch(`${EVENT_BUS_URL}/events/stream`, {
      headers: { Accept: "text/event-stream" },
    });

    if (!upstream.ok || !upstream.body) {
      return NextResponse.json({ error: "Event bus stream unavailable" }, { status: 503 });
    }

    return new NextResponse(upstream.body, {
      headers: {
        "Content-Type": "text/event-stream",
        "Cache-Control": "no-cache",
        Connection: "keep-alive",
        "X-Accel-Buffering": "no",
      },
    });
  } catch {
    return NextResponse.json({ error: "Event bus unavailable" }, { status: 503 });
  }
}
