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

/**
 * DTO used to fetch records filtered by both the user type and association type.
 *
 * This is useful when you need to retrieve data specific to a certain user role
 * (e.g., Employee, Admin) and associated entity (e.g., Institute, Branch).
 */
export class FetchByUsersDto {
  /**
   * Filter records by user type.
   *
   * Must be one of the values defined in the `ByUserType` enum.
   * For example, `ByUserType.EMPLOYEE` or `ByUserType.ADMIN`.
   *
   * Validation rules:
   * - Must not be empty
   * - Must be a valid enum value from `ByUserType`
   *
   * Example payload:
   * ```json
   * {
   *   "by": "EMPLOYEE"
   * }
   * ```
   */
  @ApiProperty({
    description: 'User type for filtering records',
    enum: ByUserType,
  })
  @IsNotEmpty({ message: 'User type (by) is required' })
  @IsIn(Object.values(ByUserType), { message: 'Invalid user type' })
  by: ByUserType;

  /**
   * Filter records by association type.
   *
   * Must be a numeric value from the `AssociationType` enum.
   * Example values might be `1` for Institute, `2` for Branch, etc.
   *
   * Validation rules:
   * - Must not be empty
   * - Must be an integer
   * - Must be a valid numeric enum value from `AssociationType`
   *
   * Example payload:
   * ```json
   * {
   *   "type": 1
   * }
   * ```
   */
  @ApiProperty({
    description: 'Numeric association type for filtering records',
    enum: AssociationType,
    example: AssociationType.INSTITUTE,
  })
  @IsNotEmpty({ message: 'Association type is required' })
  @Type(() => Number) // Ensures value is converted to number
  @IsInt({ message: 'Association type must be an integer' })
  @IsIn(Object.values(AssociationType).filter((v) => typeof v === 'number'), {
    message: 'Invalid association type',
  })
  type: AssociationType;
}
