// is-username-or-email.validator.ts
import {
  ValidatorConstraint,
  ValidatorConstraintInterface,
  ValidationArguments,
} from 'class-validator';
import { SystemLang } from '../lang/system.lang';

@ValidatorConstraint({ name: 'isUsernameOrEmail', async: false })
export class IsUsernameOrEmailConstraint
  implements ValidatorConstraintInterface
{
  validate(value: string, _args: ValidationArguments) {
    const isEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
    const isUsername = /^[a-zA-Z0-9_.-]+$/.test(value); // tùy chỉnh theo logic username bạn muốn
    const isPhone =
      /^(0|\+84)(2\d{9}|3\d{8}|5\d{8}|7\d{8}|8\d{8}|9\d{8})$/.test(value);

    return isEmail || isUsername || isPhone;
  }

  defaultMessage(_args: ValidationArguments) {
    return SystemLang.getText('messages', 'login_error_valid');
  }
}
