import { NextResponse } from "next/server";

import { getFooterPages } from "@/app/lib/pages";

// Always read the table at request time (never prerendered) — the whole
// point is that a row added to `pages` appears on the live site without
// a rebuild. The CDN/browser cache below keeps the DB from being hit on
// every page view.
export const dynamic = "force-dynamic";

export async function GET() {
  try {
    const pages = await getFooterPages();

    return NextResponse.json(
      { pages },
      { headers: { "Cache-Control": "public, s-maxage=60, stale-while-revalidate=300" } },
    );
  } catch (error) {
    console.error("Footer pages error:", error);

    // The footer falls back to its built-in links on a non-OK response.
    return NextResponse.json({ pages: [], message: "Unable to load pages." }, { status: 500 });
  }
}
