import { IsIn, IsInt, IsNotEmpty } from 'class-validator';
import { Type } from 'class-transformer';
import { AssociationType } from 'src/utils/enums/enum';
import { ApiProperty } from '@nestjs/swagger';

/**
 * DTO used to fetch records filtered by a specific association type.
 * The association type determines the category or entity (e.g., Institute, Branch)
 * for which the records should be retrieved.
 */
export class FetchByAssociationTypesDto {
  /**
   * Type of association to filter by.
   *
   * This should be one of the numeric values defined in the `AssociationType` enum.
   * For example, `AssociationType.INSTITUTE` might be `1`, while `AssociationType.BRANCH` might be `2`.
   *
   * Validation rules:
   * - Must not be empty
   * - Must be an integer
   * - Must be a valid value from the `AssociationType` enum
   *
   * Example usage:
   * ```json
   * {
   *   "type": 1
   * }
   * ```
   */
  @ApiProperty({
    description: 'Numeric association type to filter records',
    enum: AssociationType,
    example: AssociationType.INSTITUTE,
  })
  @IsNotEmpty({ message: 'Association type is required' })
  @Type(() => Number) // Ensures that incoming value is transformed to a number
  @IsInt({ message: 'Association type must be an integer' })
  @IsIn(Object.values(AssociationType).filter((v) => typeof v === 'number'), {
    message: 'Invalid association type',
  })
  type: AssociationType;
}
