import { APIRoute } from "astro";
import { getAllPlanets } from "../../../lib/db/planets";
import { ObjectId } from "mongodb";
import locationManager from "../../../lib/classes/managers/LocationManager";

export const GET: APIRoute = async ({ request }) => {
    const planets = await getAllPlanets();

    const response: Array<{planetId: ObjectId, ownerId: ObjectId, name: string}> = [];

    planets.forEach(planet => {
        const playerPlanet = locationManager.getPlanet(planet._id);

        if(!playerPlanet) return;

        response.push({
            planetId: planet._id,
            ownerId: playerPlanet.system.data.ownedBy.id,
            name: planet.name,
        })
    });

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