import { NextRequest, NextResponse } from "next/server";
import { joomlaService } from "@/services/joomlaService";

/**
 * Backs the `/blog` listing page's "Load More" button. The live site's own
 * version of this (`index.php?option=com_contact&task=contact.getBlogData`)
 * loads 10 posts per click via AJAX against a controller this app doesn't
 * have — this is the same real bridge data (`type=blog`, `JM_content`
 * catid 190), just served through our own route instead.
 */
export async function GET(request: NextRequest) {
  const offset = Number(request.nextUrl.searchParams.get("offset")) || 0;
  const limit = Number(request.nextUrl.searchParams.get("limit")) || 10;
  const sort = request.nextUrl.searchParams.get("sort") === "hits" ? "hits" : "created";

  const { data, total } = await joomlaService.getBlogPosts(limit, offset, sort);
  return NextResponse.json({ data, total });
}
