import { ObjectId } from "mongodb";
import { Systems } from "./mongodb";
import DBSystem from "../../types/db/DBSystem";
import Asteroid from "../../types/Asteroid";

export const getAllSystems = async () => {
    return await (await Systems()).find({}).toArray() as DBSystem[];
}

export const getSystemById = async (id: ObjectId) => {
    return await (await Systems()).findOne({
        _id: id
    }) as DBSystem;
}

export const createSystem = async (system: DBSystem) => {
    const systems = await Systems();
    return await systems.insertOne(system);
}

export const updateSystemStructures = async (systemId: ObjectId, structures: Array<{ id: string, level: number }>) => {
    const systems = await Systems();
    await systems.updateOne({ _id: systemId }, {
        $set: {
            structures
        }
    });
}

export const updateSystemResources = async (systemId: ObjectId, resources: Array<any>) => {
    const systems = await Systems();
    await systems.updateOne({ _id: systemId }, {
        $set: {
            resources
        }
    });
}

export const updateSystemShips = async (systemId: ObjectId, ships: Array<any>) => {
    const systems = await Systems();
    await systems.updateOne({ _id: systemId }, {
        $set: {
            ships
        }
    });
}

export const updateSystemDefenses = async (systemId: ObjectId, defenses: Array<any>) => {
    const systems = await Systems();
    await systems.updateOne({ _id: systemId }, {
        $set: {
            defenses
        }
    });
}

export const updateSystemAsteroids = async (systemId: ObjectId, asteroids: Array<Asteroid>) => {
    const systems = await Systems();
    await systems.updateOne({ _id: systemId }, {
        $set: {
            asteroids
        }
    });
}