import { Expose, Type } from 'class-transformer';
import { IsDefined, IsNumber, IsOptional, IsString, MaxLength, Min, MinLength, ValidateIf } from 'class-validator';
import { isNil } from 'lodash';
import { DefaultValue } from './decorators/defaultValueDecorator';
import { IsDefinedAllowingNull } from './decorators/isDefinedAllowingNull';
import { ValidationClass } from './validationClass';

export class FilterableMandatoryCursorOrPagePaginableVC extends ValidationClass {
    @IsOptional()
    @Expose()
    @IsString()
    @MinLength(1)
    q?: string;

    @IsDefined()
    @Expose()
    @IsNumber()
    @Type(() => Number)
    @Min(1)
    limit!: number;

    @IsDefinedAllowingNull()
    @ValidateIf((param: FilterableMandatoryCursorOrPagePaginableVC) => isNil(param.page))
    @Expose()
    @IsString()
    @MaxLength(1500)
    @DefaultValue(null)
    cursor!: string | null;

    @IsOptional()
    @ValidateIf((param: FilterableMandatoryCursorOrPagePaginableVC) => param.cursor === null)
    @Expose()
    @IsNumber()
    @Type(() => Number)
    @Min(1)
    page!: number;
}
