"use server";

import { prisma, Prisma, Changelog } from "@repo/db";
export const upsertChangelog = async (
	changelog: Prisma.ChangelogCreateInput,
	id?: Changelog["id"],
	options?: {
		skipUserUpdate?: boolean;
	},
) => {
	const newChangelog = id
		? await prisma.changelog.update({
				where: { id: id },
				data: changelog,
			})
		: await prisma.$transaction(async (prisma) => {
				const createdChangelog = await prisma.changelog.create({ data: changelog });
				if (!options?.skipUserUpdate) {
					// Update all users to acknowledge the new changelog

					await prisma.user.updateMany({
						data: { changelogAck: false },
					});
				}

				return createdChangelog;
			});
	return newChangelog;
};

export const deleteChangelog = async (id: Changelog["id"]) => {
	await prisma.changelog.delete({ where: { id: id } });
};
