import {
  registerDecorator,
  type ValidationArguments,
  type ValidationOptions,
  ValidatorConstraint,
  type ValidatorConstraintInterface,
} from 'class-validator';
import { isValidVietnamPhone } from '../vietnam-phone.validator';

@ValidatorConstraint({ name: 'isVietnamPhone', async: false })
export class IsVietnamPhoneConstraint implements ValidatorConstraintInterface {
  validate(value: unknown): boolean {
    return typeof value === 'string' && isValidVietnamPhone(value);
  }

  defaultMessage(args: ValidationArguments): string {
    return `${args.property} phải là số điện thoại Việt Nam hợp lệ (VD: 0912345678, +84912345678)`;
  }
}

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