import { ConnectedAircraft, Mission, prisma } from "@repo/db";
import { io } from "index";
import { sendNtfyMission } from "modules/ntfy";

export const sendAlert = async (
	id: number,
	{
		stationId,
	}: {
		stationId?: number;
	},
): Promise<{
	connectedAircrafts: ConnectedAircraft[];
	mission: Mission;
}> => {
	const mission = await prisma.mission.findUnique({
		where: { id: id },
	});
	const Stations = await prisma.station.findMany({
		where: {
			id: {
				in: mission?.missionStationIds,
			},
		},
	});

	if (!mission) {
		throw new Error("Mission not found");
	}

	// connectedAircrafts the alert is sent to
	const connectedAircrafts = await prisma.connectedAircraft.findMany({
		where: {
			stationId: stationId
				? stationId
				: {
						in: mission.missionStationIds,
					},
			logoutTime: null,
		},
		include: {
			Station: true,
		},
	});

	for (const aircraft of connectedAircrafts) {
		console.log(`Sending mission to: station:${aircraft.stationId}`);
		io.to(`station:${aircraft.stationId}`).emit("mission-alert", {
			...mission,
			Stations,
		});
		const user = await prisma.user.findUnique({
			where: { id: aircraft.userId },
		});
		if (!user) continue;
		if (user.settingsNtfyRoom) {
			await sendNtfyMission(
				mission,
				Stations,
				aircraft.Station,
				user.settingsNtfyRoom,
			);
		}
		const existingMissionOnStationUser =
			await prisma.missionOnStationUsers.findFirst({
				where: {
					missionId: mission.id,
					userId: aircraft.userId,
					stationId: aircraft.stationId,
				},
			});
		if (!existingMissionOnStationUser)
			await prisma.missionOnStationUsers.create({
				data: {
					missionId: mission.id,
					userId: aircraft.userId,
					stationId: aircraft.stationId,
				},
			});
	}

	// for statistics only
	await prisma.missionsOnStations
		.createMany({
			data: mission.missionStationIds.map((stationId) => ({
				missionId: mission.id,
				stationId,
			})),
		})
		.catch(() => {
			// Ignore if the entry already exists
		});

	await prisma.mission.update({
		where: { id: Number(id) },
		data: {
			state: "running",
		},
	});
	return { connectedAircrafts, mission };
};
