import { prisma } from "@repo/db";
import { eventCompleted } from "@repo/shared-components";
import { Router } from "express";
import { handleParticipantEnrolled, handleParticipantFinished } from "modules/event";
import { getMoodleCourseCompletionStatus } from "modules/moodle";

const router: Router = Router();

router.post("/handle-participant-finished", async (req, res) => {
	const { participantId } = req.body;
	if (!participantId) {
		res.status(400).json({ error: "Participant ID is required" });
		return;
	}
	const participant = await prisma.participant.findUnique({
		where: {
			id: typeof participantId == "string" ? parseInt(participantId) : participantId,
		},
		include: {
			Event: true,
			User: {
				include: {
					discordAccounts: true,
				},
			},
		},
	});
	if (!participant) {
		res.status(404).json({ error: "Participant not found" });
		return;
	}
	const completed = eventCompleted(participant.Event, participant);
	if (!completed) {
		res.status(400).json({ error: "Event not completed" });
		return;
	}
	await handleParticipantFinished(participant.Event, participant, participant.User);
	res.status(200).json({ message: "Participant finished successfully" });
});

router.post("/check-moodle-results", async (req, res) => {
	const { participantId } = req.body;
	if (!participantId) {
		res.status(400).json({ error: "Participant ID is required" });
		return;
	}
	const participant = await prisma.participant.findUnique({
		where: {
			id: typeof participantId == "string" ? parseInt(participantId) : participantId,
		},
		include: {
			Event: true,
			User: true,
		},
	});
	if (!participant) {
		res.status(404).json({ error: "Teilnehmer nicht gefunden" });
		return;
	}
	if (!participant.User || !participant.User.moodleId) {
		res.status(400).json({ error: "Teilnehmer hat keine Moodle-ID" });
		return;
	}
	const courseStatus = await getMoodleCourseCompletionStatus(
		participant.User.moodleId.toString(),
		participant.Event.finisherMoodleCourseId!,
	);

	if (courseStatus?.completionstatus?.completed === true) {
		prisma.participant.update({
			where: { id: participant.id },
			data: { finisherMoodleCurseCompleted: true },
		});
		await handleParticipantFinished(participant.Event, participant, participant.User);
		res
			.status(200)
			.json({ message: "Nutzer hat das Moodle-event abgeschlossen, workflow ausgeführt" });
	} else {
		res.status(200).json({ message: "Nutzer hat das Moodle-event noch nicht abgeschlossen" });
	}
});

router.post("/handle-participant-enrolled", async (req, res) => {
	const { participantId } = req.body;
	if (!participantId) {
		res.status(400).json({ error: "Participant ID is required" });
		return;
	}
	const participant = await prisma.participant.findUnique({
		where: {
			id: typeof participantId == "string" ? parseInt(participantId) : participantId,
		},
		include: {
			Event: true,
			User: {
				include: {
					discordAccounts: true,
				},
			},
		},
	});
	if (!participant) {
		res.status(404).json({ error: "Participant not found" });
		return;
	}
	const completed = eventCompleted(participant.Event, participant);
	if (completed) {
		res.status(400).json({ error: "Event already completed" });
		return;
	}
	await handleParticipantEnrolled(participant.Event, participant, participant.User);
	res.status(200).json({ message: "Participant enrolled successfully" });
});

export default router;
