import { registerDecorator, ValidationOptions, ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } from 'class-validator';

@ValidatorConstraint({ name: 'IsFutureDateAllowingNull', async: false })
export class IsFutureDateAllowingNullConstraint implements ValidatorConstraintInterface {
    validate(value: any): boolean {
        if (value === null || value === undefined) return true;
        const date = value instanceof Date ? value : new Date(value as string);
        return !isNaN(date.getTime()) && date.getTime() > Date.now();
    }

    defaultMessage(args: ValidationArguments): string {
        return `The date must be in the future if specified.`;
    }
}

export function IsFutureDateAllowingNull(validationOptions?: ValidationOptions): PropertyDecorator {
    return (target: object, propertyName: string | symbol) => {
        registerDecorator({
            name: 'IsFutureDateAllowingNull',
            options: validationOptions,
            propertyName: propertyName.toString(),
            target: target.constructor,
            validator: IsFutureDateAllowingNullConstraint,
        });
    };
}
