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 for fetching data based on a specific association type.
 *
 * This DTO ensures that only valid association types are accepted,
 * enforcing numeric type and membership in the predefined `AssociationType` enum.
 */
export class FetchByAssociationTypesDto {
  /**
   * The type of association to filter by.
   *
   * Examples of association types could be:
   * - INSTITUTE
   * - SUB_AGENT
   * - OTHER types defined in `AssociationType` enum
   *
   * Validation:
   * - Must not be empty (`@IsNotEmpty`)
   * - Must be an integer (`@IsInt`)
   * - Must be one of the valid enum values (`@IsIn`)
   *
   * Transformation:
   * - Uses `@Type(() => Number)` to ensure that the value is converted to a number
   *   if it comes from a string (common when receiving query parameters).
   *
   * Swagger Documentation:
   * - `enum` shows all valid `AssociationType` values
   * - `example` provides a sample default value for API docs (e.g., `INSTITUTE`)
   */
  @ApiProperty({
    enum: AssociationType,
    example: AssociationType.INSTITUTE,
  })
  @IsNotEmpty() // Value is required; cannot be null or undefined
  @Type(() => Number) // Converts input to number (important for query params)
  @IsInt() // Ensures the value is an integer
  @IsIn(Object.values(AssociationType).filter((v) => typeof v === 'number')) // Ensures the value matches one of the numeric enum values
  type: AssociationType;
}
