import { ConnectedAircraft, getPublicUser, MissionLog, Prisma, prisma } from "@repo/db";
import { Router } from "express";
import { io } from "../index";

const router: Router = Router();

// Get all connectedAircrafts
router.post("/", async (req, res) => {
	try {
		const filter = req.body?.filter || {};
		const connectedAircrafts = await prisma.connectedAircraft.findMany({
			where: filter,
		});
		res.json(connectedAircrafts);
	} catch (error) {
		console.error(error);
		res.status(500).json({ error: "Failed to fetch connectedAircrafts" });
	}
});

// Get a single connectedAircraft by ID
router.get("/:id", async (req, res) => {
	const { id } = req.params;
	try {
		const connectedAircraft = await prisma.connectedAircraft.findUnique({
			where: { id: Number(id) },
		});
		if (connectedAircraft) {
			res.json(connectedAircraft);
		} else {
			res.status(404).json({ error: "ConnectedAircraft not found" });
		}
	} catch (error) {
		console.error(error);
		res.status(500).json({ error: "Failed to fetch connectedAircraft" });
	}
});

// Update a connectedAircraft by ID
router.patch("/:id", async (req, res) => {
	const { id } = req.params;
	const aircraftUpdate = req.body as Prisma.ConnectedAircraftUpdateInput;
	try {
		const oldConnectedAircraft = await prisma.connectedAircraft.findUnique({
			where: { id: Number(id) },
			include: {
				Station: true,
				User: true,
			},
		});
		const updatedConnectedAircraft = await prisma.connectedAircraft.update({
			where: { id: Number(id) },
			data: {
				...aircraftUpdate,
			},
		});

		const mission = await prisma.mission.findFirst({
			where: {
				state: "running",
				missionStationIds: {
					has: updatedConnectedAircraft.stationId,
				},
			},
		});
		if (
			mission &&
			aircraftUpdate.fmsStatus &&
			oldConnectedAircraft &&
			updatedConnectedAircraft &&
			oldConnectedAircraft.fmsStatus !== updatedConnectedAircraft.fmsStatus
		) {
			const newMissionLog = mission.missionLog as any as MissionLog[];
			newMissionLog.push({
				type: "station-log",
				auto: true,
				data: {
					oldFMSstatus: oldConnectedAircraft.fmsStatus,
					newFMSstatus: updatedConnectedAircraft.fmsStatus,
					station: oldConnectedAircraft.Station,
					stationId: updatedConnectedAircraft.stationId,
					user: getPublicUser(oldConnectedAircraft.User),
				},
				timeStamp: new Date().toISOString(),
			});
			await prisma.mission.update({
				where: { id: mission.id },
				data: {
					missionLog: newMissionLog as any,
				},
			});
		}

		io.to("dispatchers").emit("update-connectedAircraft", updatedConnectedAircraft);
		io.to(`user:${updatedConnectedAircraft.userId}`).emit(
			"aircraft-update",
			updatedConnectedAircraft,
		);
		res.json(updatedConnectedAircraft);
	} catch (error) {
		console.error(error);
		res.status(500).json({ error: "Failed to update connectedAircraft" });
	}
});

// Delete a connectedAircraft by ID
router.delete("/:id", async (req, res) => {
	const { id } = req.params;
	try {
		await prisma.connectedAircraft.delete({
			where: { id: Number(id) },
		});
		io.to("dispatchers").emit("delete-connectedAircraft", id);
		res.status(204).send();
	} catch (error) {
		console.error(error);
		res.status(500).json({ error: "Failed to delete connectedAircraft" });
	}
});

export default router;
