import { NextRequest, NextResponse } from "next/server";
import { getMerchantSuggestions, getBankNames } from "@/lib/queries";
import { getCurrentUser } from "@/lib/auth";

export async function GET(req: NextRequest) {
  const user = await getCurrentUser(req);
  if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
  const search = req.nextUrl.searchParams.get("search");
  const type = req.nextUrl.searchParams.get("type");

  if (type === "banks") {
    const banks = await getBankNames();
    return NextResponse.json(banks);
  }

  if (!search) return NextResponse.json([]);
  const merchants = await getMerchantSuggestions(search);
  return NextResponse.json(merchants.map((m) => m.merchant));
}
