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

export function IsDateTimeString(validationOptions?: ValidationOptions) {
    return function (object: Object, propertyName: string) {
        registerDecorator({
            name: 'isDateTimeString',
            target: object.constructor,
            propertyName: propertyName,
            options: validationOptions,
            validator: {
                validate(value: any, args: ValidationArguments) {
                    // Check if the value matches the YYYY-MM-DD HH:mm:ss format
                    const dateTimeRegex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
                    if (!dateTimeRegex.test(value)) {
                        return false;
                    }

                    // Extract the month part from the value
                    const month = parseInt(value.split('-')[1], 10);

                    // Ensure the month does not exceed 12
                    return month >= 1 && month <= 12;
                },
                defaultMessage(args: ValidationArguments) {
                    return `${args.property} must be a valid datetime string in the format YYYY-MM-DD HH:mm:ss and the month must not exceed 12`;
                }
            }
        });
    };
}
