import { Injectable } from "@nestjs/common";
import { registerDecorator, ValidationOptions, ValidatorConstraint, ValidatorConstraintInterface } from "class-validator";
import { UsersService } from "src/modules/users/users.service";

@ValidatorConstraint({ name: 'IsUserEmailUnique', async: true })
@Injectable()
export class IsPhoneOnlyNumberRule implements ValidatorConstraintInterface {
    constructor(private readonly usersService: UsersService) {}

    async validate(payload: string): Promise<boolean> {
        return /^\d+$/.test(payload);
    }

    defaultMessage() {
        return `Nomor telepon hanya diijinkan angka.`;
    }
}

export function IsPhoneOnlyNumber(validationOptions?: ValidationOptions) {
    return function (object: any, propertyName: string) {
        registerDecorator({
            target: object.constructor,
            propertyName: propertyName,
            options: validationOptions,
            constraints: [],
            validator: IsPhoneOnlyNumberRule,
        })
    }
}