import { Planets } from '../db/mongodb';
import { ObjectId } from 'mongodb';
import DBPlanet from '../../types/db/DBPlanet';

export const getAllPlanets = async () => {
    return await (await Planets()).find({}).toArray() as DBPlanet[];
}

export const getPlanetById = async (id: ObjectId) => {
    return await (await Planets()).findOne({ _id: id }) as DBPlanet;
}

export const createPlanet = async (planet: DBPlanet) => {
    const planets = await Planets();
    return await planets.insertOne(planet);
}

export const updatePlanetResources = async (planetId: ObjectId, resources: Array<any>) => {
    const planets = await Planets();
    await planets.updateOne({ _id: planetId }, {
        $set: {
            resources
        }
    });
}

export const updatePlanetBuildings = async (planetId: ObjectId, buildings: Array<{ id: string, level: number }>) => {
    const planets = await Planets();
    await planets.updateOne({ _id: planetId }, {
        $set: {
            buildings
        }
    });
}

export const updatePlanetShips = async (planetId: ObjectId, ships: Array<{ id: string, amount: number }>) => {
    const planets = await Planets();
    await planets.updateOne({ _id: planetId }, {
        $set: {
            ships
        }
    });
}

export const updatePlanetDefenses = async (planetId: ObjectId, defenses: Array<{ id: string, amount: number }>) => {
    const planets = await Planets();
    await planets.updateOne({ _id: planetId }, {
        $set: {
            defenses
        }
    });
}