/*
 * WHAT IS THIS FILE?
 *
 * It's the entry point for the Bun HTTP server when building for production.
 *
 * Learn more about the Bun integration here:
 * - https://qwik.dev/docs/deployments/bun/
 * - https://bun.sh/docs/api/http
 *
 */
import { getRequestEvent } from "@qwik.dev/router";
import { createQwikRouter } from "@qwik.dev/router/middleware/bun";
import render from "./entry.ssr";

// Create the Qwik Router Bun middleware
const { router, staticFile } = createQwikRouter({
  render,
  static: {
    cacheControl: "public, max-age=31536000, immutable",
  },
});

// Allow for dynamic port
const port = Number(Bun.env.PORT ?? 3000);

// eslint-disable-next-line no-console
console.log(`Server started: http://localhost:${port}/`);

// Optional request-aware diagnostics for crashes that escape request boundaries.
// This does not prevent Bun from crashing, but it does provide better diagnostics.
// See Bun and Node process event docs for runtime-specific behavior.
process.on("uncaughtException", (error) => {
  const requestEv = getRequestEvent();
  if (requestEv) {
    console.error("Unhandled exception during request", {
      method: requestEv.method,
      url: requestEv.url.href,
      headersSent: requestEv.headersSent,
      error,
    });
    return;
  }

  console.error("Unhandled exception outside request", { error });
});

process.on("unhandledRejection", (reason) => {
  const requestEv = getRequestEvent();
  if (requestEv) {
    console.error("Unhandled rejection during request", {
      method: requestEv.method,
      url: requestEv.url.href,
      headersSent: requestEv.headersSent,
      reason,
    });
    return;
  }

  console.error("Unhandled rejection outside request", { reason });
});

Bun.serve({
  maxRequestBodySize: 10 * 1024 * 1024,
  async fetch(request: Request) {
    const staticResponse = await staticFile(request);
    if (staticResponse) {
      return staticResponse;
    }

    // Server-side render this request with Qwik Router
    return (await router(request))!;
  },
  port,
});
