import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";

export async function GET(req: Request) {
  const { searchParams } = new URL(req.url);
  const category = searchParams.get("category") || undefined;
  const categoryId = searchParams.get("categoryId") || undefined;
  let where: any = {};
  if (categoryId) {
    // 카테고리 ID 매칭 + 과거 데이터 호환(문자열 category명 매칭)
    let catName: string | undefined;
    try {
      const cat = await prisma.partnerCategory.findUnique({ where: { id: categoryId }, select: { name: true } });
      catName = cat?.name;
    } catch {}
    where = catName ? { OR: [{ categoryId }, { category: catName }] } : { categoryId };
  } else if (category) {
    where = { category };
  }
  const partners = await prisma.partner.findMany({ where, orderBy: { createdAt: "desc" } });
  return NextResponse.json({ partners });
}


