import { Transform } from 'class-transformer';
import {
  ArrayMaxSize,
  ArrayNotEmpty,
  ArrayUnique,
  IsArray,
  IsInt,
  IsNotEmpty,
  IsString,
  Max,
  MaxLength,
  Min,
  ValidateIf,
} from 'class-validator';
import { TransformNumberArray } from '../../../shared/decorators/class-transformer/transform-number-array.decorator';
import { TransformNumber } from '../../../shared/decorators/class-transformer/transform-number.decorator';
import { SimpleFood } from '../../../shared/dtos/foods/simple-food.dto';
import { IsOptional } from '../../../shared/decorators/class-validator/is-optional.decorator';
import { MatchesNamesRegex } from '../../../shared/decorators/class-validator/matches-names-regex.decorator';

export class RetrieveFoodsQueryParamsDto {
  @ValidateIf((object: RetrieveFoodsQueryParamsDto) => !object.ids)
  @Transform(({ obj, value }) => (!obj.ids ? value : undefined))
  @IsOptional()
  @TransformNumber()
  @IsInt()
  @Min(1)
  page: number = 1;

  @ValidateIf((object: RetrieveFoodsQueryParamsDto) => !object.ids)
  @Transform(({ obj, value }) => (!obj.ids ? value : undefined))
  @IsOptional()
  @TransformNumber()
  @IsInt()
  @Min(1)
  @Max(20)
  items_per_page: number = 20;

  @ValidateIf((object: RetrieveFoodsQueryParamsDto) => !object.ids)
  @Transform(({ obj, value }) => (!obj.ids ? value : undefined))
  @IsOptional()
  @IsString()
  @IsNotEmpty()
  @MaxLength(80)
  @MatchesNamesRegex()
  search_by_name?: string;

  @IsOptional()
  @TransformNumberArray()
  @IsArray()
  @ArrayUnique()
  @ArrayNotEmpty()
  @ArrayMaxSize(20)
  @IsInt({ each: true })
  @Min(1, { each: true })
  ids?: number[];
}

export class RetrieveFoodsResponseDto {
  data: SimpleFood[];
  actualPage?: number;
  totalPages?: number;
}
