import { prisma } from '@hexahost/database';

export async function releaseServerAllocation(serverId: string): Promise<void> {
  const allocation = await prisma.gameServerAllocation.findUnique({
    where: { serverId },
  });

  if (!allocation || allocation.status === 'RELEASED') {
    return;
  }

  await prisma.$transaction([
    prisma.gameServerAllocation.update({
      where: { serverId },
      data: { status: 'RELEASED' },
    }),
    prisma.gameNode.updateMany({
      where: { id: allocation.nodeId, activeServers: { gt: 0 } },
      data: { activeServers: { decrement: 1 } },
    }),
  ]);
}
