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

export async function GET(request: NextRequest): Promise<NextResponse> {
	try {
		const connectedAircraftId = request.nextUrl.searchParams.get("connectedAircraftId");

		if (!connectedAircraftId)
			return NextResponse.json({ error: "connectedAircraftId is required" }, { status: 400 });

		const positionLog = await prisma.positionLog.findMany({
			where: {
				connectedAircraftId: Number(connectedAircraftId),
				timestamp: {
					gte: new Date(Date.now() - 2 * 60 * 60 * 1000), // Last 2 hours
				},
			},
			orderBy: {
				timestamp: "desc",
			},
		});

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