import { registerDecorator, ValidationArguments, ValidationOptions } from 'class-validator';
import { ShiftRotationDayType } from '../../../../business/enum/shiftManagement/shiftRotationDayType';

export function IsValidShiftRotationDay(validationOptions?: ValidationOptions) {
    return function (object: object, propertyName: string) {
        registerDecorator({
            constraints: [],
            name: 'IsValidShiftRotationDay',
            options: validationOptions,
            propertyName,
            target: object.constructor,
            validator: {
                defaultMessage(_args: ValidationArguments) {
                    return 'WORK day must have between 1 and 3 shifts; REST day must have 0 shifts';
                },
                validate(value: unknown, args: ValidationArguments) {
                    const day = args.object as { type?: unknown; shifts?: unknown };
                    const type = day.type;
                    const shiftsRaw = day.shifts;
                    if (type !== ShiftRotationDayType.WORK && type !== ShiftRotationDayType.REST) return false;
                    if (!Array.isArray(shiftsRaw)) return false;
                    const shiftCount = shiftsRaw.length;
                    if (type === ShiftRotationDayType.WORK) return shiftCount >= 1 && shiftCount <= 3;
                    return shiftCount === 0;
                },
            },
        });
    };
}
