import { ISO_DATE, OptionalNumbersArrayOrStringToNumbersArray } from '@humand-packages/common';
import { Expose, Transform, Type } from 'class-transformer';
import { IsArray, IsDefined, IsEnum, IsNumber, IsOptional, IsString, IsUUID, MaxLength } from 'class-validator';
import { isArray, isString, toNumber } from 'lodash';
import { IsValidDate } from '../../../../../middlewares/dateValidator';
import { SearchType } from '../../../business/types/shiftManagement/searchType';

export class ShiftManagementCalendarUserFilterVC {
    @IsOptional()
    @IsArray()
    @IsNumber({}, { each: true })
    @Expose()
    @OptionalNumbersArrayOrStringToNumbersArray()
    userIds?: number[];

    @IsOptional()
    @IsString()
    @MaxLength(255)
    @Expose()
    search?: string;

    @IsNumber()
    @Expose()
    @Type(() => Number)
    @IsOptional()
    siteId?: number;

    @IsOptional()
    @IsArray()
    @IsNumber({}, { each: true })
    @Expose()
    @Transform(({ value }) => {
        if (!value) return [];
        if (isArray(value)) return value.map((item) => toNumber(item));
        if (isString(value)) return (value as string).split(',').map((stringNumber) => toNumber(stringNumber));
        return [];
    })
    segmentationItemIds?: number[];

    @IsOptional()
    @IsEnum(SearchType)
    @Expose()
    @Transform(({ value }) => (value === undefined ? SearchType.ALL : (value as SearchType)))
    searchType?: SearchType;

    @IsOptional()
    @IsArray()
    @IsUUID(undefined, { each: true })
    @Expose()
    @Transform(({ value }) => {
        if (!value) return undefined;
        if (isArray(value)) return value;
        if (isString(value)) return (value as string).split(',').map((id) => id.trim());
        return undefined;
    })
    shiftRotationIds?: string[];
}

export class ShiftManagementCalendarFiltersVC extends ShiftManagementCalendarUserFilterVC {
    @IsDefined()
    @IsValidDate(ISO_DATE)
    @Expose()
    startDate!: string;

    @IsDefined()
    @IsValidDate(ISO_DATE)
    @Expose()
    endDate!: string;
}
