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

export const addMessage = async (message: Prisma.NotamCreateInput) => {
	try {
		// Set all current messages with isMainMsg=true to active=false
		await prisma.notam.updateMany({
			where: { isMainMsg: true },
			data: { active: false },
		});

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

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