import { ObjectId } from "mongodb";
import { SpyReports } from "./mongodb"
import DBSpyReport, { DBSpyReportPlanet } from "../../types/db/DBSpyReport";
import { addSpyReportToUser } from "./users";

export const getSpyReportById = async (id: ObjectId) => {
    return await (await SpyReports()).findOne({
        _id: id
    }) as DBSpyReport;
}

export const getSpyReportBySystemId = async (id: ObjectId) => {
    return await (await SpyReports()).findOne({
        systemId: id
    }) as DBSpyReport;
}

export const addSpyReport = async (
    toPlayer: ObjectId,
    victimId: ObjectId,
    scannedAt: Date,
    research: Array<{ id: string, level: number }>,
    data: {
        systemId: ObjectId,
        resources: Array<{ id: string, amount: number }>,
        structures: Array<{ id: string, level: number }>,
        ships: Array<{ id: string, amount: number }>,
        defense: Array<{ id: string, amount: number }>,
        planets: Array<DBSpyReportPlanet>
    }
) => {
    const report = await (await SpyReports()).insertOne({
        victimId,
        scannedAt,
        research,
        systemId: data.systemId,
        resources: data.resources,
        structures: data.structures,
        ships: data.ships,
        defense: data.defense,
        planets: data.planets
    });

    await addSpyReportToUser(toPlayer, report.insertedId);
}

export const updateSystemSpyReportWithoutPlanets = async (
    id: ObjectId,
    resources: Array<{ id: string, amount: number }>,
    structures: Array<{ id: string, level: number }>,
    ships: Array<{ id: string, amount: number }>,
    defense: Array<{ id: string, amount: number }>
) => {
    await (await SpyReports()).updateOne({
        _id: id
    }, {
        $set: {
            resources,
            structures,
            ships,
            defense
        }
    });
}

export const addPlanetToExistingSpyReport = async (
    id: ObjectId,
    data: {
        resources: Array<{ id: string, amount: number }>,
        buildings: Array<{ id: string, level: number }>,
        ships: Array<{ id: string, amount: number }>,
        defense: Array<{ id: string, amount: number }>
    }
) => {
    await (await SpyReports()).updateOne({
        _id: id
    }, {
        $push: {
            planets: {
                id: new ObjectId(),
                scanned: true,
                scannedAt: new Date(),
                resources: data.resources,
                buildings: data.buildings,
                ships: data.ships,
                defense: data.defense
            }
        }
    });
}

export const updatePlanetSpyReport = async (
    id: ObjectId,
    planetId: ObjectId,
    data: {
        resources: Array<{ id: string, amount: number }>,
        buildings: Array<{ id: string, level: number }>,
        ships: Array<{ id: string, amount: number }>,
        defense: Array<{ id: string, amount: number }>
    }
) => {
    await (await SpyReports()).updateOne({
        _id: id,
        "planets.id": planetId
    }, {
        $set: {
            "planets.$.scanned": true,
            "planets.$.scannedAt": new Date(),
            "planets.$.resources": data.resources,
            "planets.$.buildings": data.buildings,
            "planets.$.ships": data.ships,
            "planets.$.defense": data.defense
        }
    });
}

export const overwriteSpyReport = async (id: ObjectId, report: DBSpyReport) => {
    await (await SpyReports()).replaceOne({
        _id: id
    }, report);
}