import { ValidationArguments, ValidatorConstraint, ValidatorConstraintInterface } from 'class-validator';
import moment from 'moment';
import { GetCategorizedHoursVC } from '../../validationClasses/hourCategorization/getCategorizedHoursVC';

@ValidatorConstraint({ async: false, name: 'IsStartDateBeforeEndDate' })
export class IsStartDateBeforeEndDateConstraint implements ValidatorConstraintInterface {
    validate(value: unknown, args: ValidationArguments): boolean {
        const obj = args.object as GetCategorizedHoursVC;
        if (!obj.startDate || !obj.endDate) {
            return true;
        }
        return moment(obj.startDate).isSameOrBefore(moment(obj.endDate));
    }

    defaultMessage(_args?: ValidationArguments): string {
        return 'startDate must be before or equal to endDate';
    }
}
