import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@repo/db";

export async function GET(req: NextRequest): Promise<NextResponse> {
	const { searchParams } = new URL(req.url);
	const id = searchParams.get("id");
	const filter = searchParams.get("filter");

	try {
		const data = await prisma.keyword.findMany({
			where: {
				id: id ? Number(id) : undefined,
				...(filter ? JSON.parse(filter) : {}),
			},
		});

		return NextResponse.json(data, { status: 200 });
	} catch (error) {
		console.error(error);
		return NextResponse.json(
			{ error: "Failed to fetch keyword" },
			{ status: 500 },
		);
	}
}
