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

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

    const user = await getUserByAccessToken(response);

    if(!user) return new Response(
        JSON.stringify({
            code: 404,
            message: "Not found",
            data: "Access token does not match any user"
        }), { status: 404 }
    );

    return new Response(
        JSON.stringify({
            code: 200,
            message: "OK",
            data: "Access token valid for user " + user.username
        })
    );
}