import { db } from "@/server/db";
import { printJobs } from "@/server/db/schema";
import { eq } from "drizzle-orm";

interface RemainingTimeRouteProps {
	params: {
		jobId: string;
	};
}
export async function GET(request: Request, { params }: RemainingTimeRouteProps) {
	// Get the job details
	const jobDetails = await db.query.printJobs.findFirst({
		where: eq(printJobs.id, params.jobId),
	});

	// Check if the job exists
	if (!jobDetails) {
		return Response.json({
			id: params.jobId,
			error: "Job not found",
		});
	}

	// Calculate the remaining time
	const startAt = new Date(jobDetails.startAt).getTime();
	const endAt = startAt + jobDetails.durationInMinutes * 60 * 1000;
	const remainingTime = Math.max(0, endAt - Date.now());

	// Return the remaining time
	return Response.json({
		id: params.jobId,
		remainingTime,
	});
}
