import { redirect, notFound } from "next/navigation";
import { NextRequest } from "next/server";
import { resolveAndTrackRedirect } from "../../lib/payload";

// Legacy sticker QR codes already printed as https://einfach-produktiv.com/sticker/<code>
// before the /r/[code] short-link scheme existed — can't be reprinted, so
// this mirrors app/r/[code]/route.ts's exact lookup (same Redirects
// collection, matched by `code` + urlPrefix "/sticker") under the fixed
// /sticker prefix instead. Keeps a single admin-editable/toggleable source
// of truth for both.
export async function GET(_request: NextRequest, { params }: { params: Promise<{ code: string }> }) {
  const { code } = await params;
  const targetPath = await resolveAndTrackRedirect(code, "/sticker");

  if (!targetPath || !targetPath.startsWith("/") || targetPath.startsWith("//")) {
    notFound();
  }

  redirect(targetPath);
}
