import { NextRequest, NextResponse } from "next/server";
import Papa from "papaparse";

export const revalidate = 3600; // 1 hour

export async function GET(req: NextRequest) {
  const url = new URL(req.url);
  const id = url.searchParams.get("id");
  if (!id) return NextResponse.json({ error: "Missing id" }, { status: 400 });
  try {
    const apiUrl = `https://www.openpowerlifting.org/api/meetcsv/apportugal/${id}`;
    const res = await fetch(apiUrl, { next: { revalidate } });
    const csv = await res.text();
    const parsed = Papa.parse(csv, { header: true, skipEmptyLines: true });
    return NextResponse.json({ data: parsed.data });
  } catch (e) {
    return NextResponse.json({ error: "Failed to fetch meet CSV" }, { status: 500 });
  }
}
