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 { IsOptional } from '../../../shared/decorators/class-validator/is-optional.decorator';
import { MatchesNamesRegex } from '../../../shared/decorators/class-validator/matches-names-regex.decorator';

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

  @IsOptional()
  @TransformNumber()
  @IsInt()
  @Min(1)
  @Max(20)
  items_per_page: number = 20;

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

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

export class RetrieveRecipesTags_Tag {
  id: number;
  name: string;
}

export class RetrieveRecipesTagsResponseDto {
  data: RetrieveRecipesTags_Tag[];
  actualPage?: number;
  totalPages?: number;
}
