import { NextResponse, type NextRequest } from "next/server";
import { supabase } from "@/lib/supabase";

type RouteParams = {
  params: Promise<{ id: string }>;
};

// Stable QR redirect target (PRD §12) — printed QR codes encode this URL and
// never change; only the row's target_url is updated when domain/slug changes.
export async function GET(_req: NextRequest, { params }: RouteParams) {
  const { id } = await params;

  const { data } = await supabase.from("qr_codes").select("target_url").eq("id", id).maybeSingle();

  if (!data) {
    return NextResponse.json({ message: "not found" }, { status: 404 });
  }

  return NextResponse.redirect(data.target_url, { status: 302 });
}
