import { type APIRoute } from "astro";
import validateAccessToken from "../../../lib/utils/validateAccessToken";
import { getUserByAccessToken, getUserResearch } from "../../../lib/db/users";
import locationManager from "../../../lib/classes/managers/LocationManager";

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 }
        )
    }

    const userResearch = locationManager.getUser(user._id)?.research.research ?? [];

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