"use server";
import { prisma, Prisma } from "@repo/db";

export const addMessage = async (message: Prisma.NotamCreateInput) => {
	try {
		await prisma.notam.updateMany({
			where: { active: true },
			data: { active: false },
		});

		const showUntil = new Date(message.showUntil);
		const showUntilActive = showUntil > new Date();

		await prisma.notam.create({
			data: {
				message: message.message,
				color: message.color,
				active: true,
				wartungsmodus: message.wartungsmodus,
				disableHPG: message.disableHPG,
				showUntilActive,
				showUntil,
			},
		});
	} catch (error) {
		throw new Error("Failed to add message");
	}
};

export const disableMessage = async () => {
	try {
		await prisma.notam.updateMany({
			where: { active: true },
			data: { active: false },
		});
	} catch (error) {
		throw new Error("Failed to disable message");
	}
};
