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

export const IsAdminPasswordvalid = (validationOptions?: ValidationOptions) => {
  return (object: any, propertyName: string) => {
    registerDecorator({
      name: 'IsAdminPasswordvalid',
      target: object.constructor,
      propertyName: propertyName,
      constraints: [],
      options: validationOptions,
      validator: {
        validate: (value: string) => {
          // 8文字～64文字でなければ早期に不合格
          const minLength = 8;
          const maxLength = 64;
          if (value.length < minLength || value.length > maxLength) {
            return false;
          }

          // 英字の大文字、英字の小文字、アラビア数字、記号（@#$%^&*\-_+=[]{}|\:',.?/`~"();!）から2種類以上組み合わせ
          const charaTypePattern =
            /^((?=.*[a-z])(?=.*[A-Z])|(?=.*[a-z])(?=.*[\d])|(?=.*[a-z])(?=.*[@#$%^&*\\\-_+=\[\]{}|:',.?\/`~"();!])|(?=.*[A-Z])(?=.*[\d])|(?=.*[A-Z])(?=.*[@#$%^&*\\\-_+=\[\]{}|:',.?\/`~"();!])|(?=.*[\d])(?=.*[@#$%^&*\\\-_+=\[\]{}|:',.?\/`~"();!]))[a-zA-Z\d@#$%^&*\\\-_+=\[\]{}|:',.?\/`~"();!]/;
          return new RegExp(charaTypePattern).test(value);
        },
        defaultMessage: () => {
          return 'Admin password rule not satisfied';
        },
      },
    });
  };
};
