"use server";
import { enrollUserInCourse } from "../../../helper/moodle";
import { Prisma, prisma } from "@repo/db";

export const inscribeToMoodleCourse = async (
	moodleCourseId: string | number,
	userMoodleId: string | number,
) => {
	await enrollUserInCourse(moodleCourseId, userMoodleId);
};

export const upsertParticipant = async (
	data: Prisma.ParticipantUncheckedCreateInput,
) => {
	const participant = await prisma.participant.findFirst({
		where: {
			userId: data.userId,
			eventId: data.eventId,
		},
	});
	if (!participant) {
		return await prisma.participant.create({
			data,
		});
	}
	return await prisma.participant.update({
		where: {
			id: participant.id,
		},
		data,
	});
};
