import {
  ForbiddenException,
  NotFoundException,
} from '@nestjs/common';
import type { GameServer } from '@hexahost/database';

import type { PrismaService } from '../../prisma/prisma.service';

export async function findOwnedServer(
  prisma: PrismaService,
  userId: string,
  serverId: string,
): Promise<GameServer> {
  const server = await prisma.gameServer.findUnique({
    where: { id: serverId },
  });

  if (!server) {
    throw new NotFoundException('Server not found');
  }

  if (server.userId !== userId) {
    throw new ForbiddenException('You do not own this server');
  }

  return server;
}
