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

export async function GET(request: Request): Promise<NextResponse> {
	try {
		const filter = JSON.parse(
			new URL(request.url).searchParams.get("filter") || "{}",
		) as Prisma.ConnectedAircraftWhereInput;

		const connectedAircraft = await prisma.connectedAircraft.findMany({
			where: {
				logoutTime: null,
				...filter, // Ensure filter is parsed correctly
			},
			include: {
				Station: true,
			},
		});

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