import { APIRoute } from "astro";
import validateAccessToken from "../../../lib/utils/validateAccessToken";
import { getUserByAccessToken } from "../../../lib/db/users";
import { getAllFleetByUser } from "../../../lib/db/fleet";
import parseParams from "../../../lib/utils/parseParams";

export const GET: APIRoute = async({ request }) => {
    const response = await validateAccessToken(request);
    if(response instanceof Response) return response;

    const user = await getUserByAccessToken(response);
    if(user === null) {
        return new Response(
            JSON.stringify({
                code: 401,
                message: "Unauthorized"
            }), { status: 401 }
        )
    }

    let allFleet = (await getAllFleetByUser(user._id)).filter(f => f.arrivalTime.getTime() > Date.now());

    const params = {

    }

    const URLParams = parseParams(request.url);

    return new Response(
        JSON.stringify({
            code: 200,
            message: "OK",
            data: allFleet
        }), { status: 200 }
    )
}