import {
  BadRequestException,
  ForbiddenException,
  NotFoundException,
} from '@nestjs/common';

export function assertRessourceBelongsToUser(
  ressourceUserId: string,
  userId: string,
) {
  if (ressourceUserId !== userId) {
    throw new ForbiddenException("Vous n'avez pas accès à cette ressource");
  }
}

export function assertRessourceExists(ressource: any) {
  if (!ressource) {
    throw new NotFoundException("Cette ressource n'existe pas");
  }
}

export function assertIdIsValid(id: string) {
  if (!id) {
    throw new BadRequestException("L'identifiant est requis");
  }
}

export function assertValuesAreDefined(values: any[]) {
  return values.every((value) => value !== undefined);
}

export const assertRessourceBelongsToUsers = (
  ressource: any,
  usersIds: string[],
) => {
  if (!usersIds.includes(ressource.userId)) {
    throw new ForbiddenException("Vous n'avez pas accès à cette ressource");
  }
};

export const assertAtLeastOneValueIsDefined = (values: any[]) => {
  if (!values.some((value) => value !== undefined)) {
    throw new BadRequestException('Paramètre manquant');
  }
};
