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

const TIME_REGEX = /^([01]\d|2[0-3]):([0-5]\d)$/;

@ValidatorConstraint({ async: false, name: 'isValidTimeSlotTuples' })
export class IsValidTimeSlotTuplesConstraint implements ValidatorConstraintInterface {
    validate(value: unknown, _args: ValidationArguments): value is [string, string][] {
        if (!Array.isArray(value)) return false;

        return value.every((slot): slot is [string, string] => {
            if (!Array.isArray(slot) || slot.length !== 2) return false;

            const start = slot[0] as unknown;
            const end = slot[1] as unknown;
            return typeof start === 'string' && typeof end === 'string' && TIME_REGEX.test(start) && TIME_REGEX.test(end);
        });
    }

    defaultMessage(): string {
        return 'Each timeSlot must be a [start, end] pair in HH:mm format';
    }
}
