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

export const validationErrorMessage = (errors: ValidationError[]) => {
  const firstError = errors[0]; // Get the first error from the array
  const validationErrorResponse = {
    status: false,
    code: 422,
    message: firstError
      ? Object.values(firstError.constraints)[0]
      : 'Validation Error', // Use the message of the first error, or default to "Validation Error" if there are no errors
  };

  return new BadRequestException(validationErrorResponse);
};

export function successResponse(
  code: number,
  message: string = '',
  data: any = [],
) {
  return {
    status: true,
    code,
    message,
    data,
  };
}

export function errorResponse(
  code: number,
  message: string = '',
  data: any = [],
) {
  return {
    status: false,
    code,
    message,
    data,
  };
}
