/**
 * PartyId Validator
 *
 * Custom validator for PartyId format validation.
 */

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

@ValidatorConstraint({ name: 'isPartyId', async: false })
export class IsPartyIdConstraint implements ValidatorConstraintInterface {
  validate(value: string, _args: ValidationArguments): boolean {
    if (!value || typeof value !== 'string') {
      return false;
    }
    // Format: {identifier}-{type} e.g., user123-server
    const partyIdRegex = /^[\w]+-[\w]+$/;
    return partyIdRegex.test(value);
  }

  defaultMessage(_args: ValidationArguments): string {
    return 'PartyId must be in format: {identifier}-{type} (e.g., user123-server)';
  }
}

/**
 * Decorator for PartyId validation
 */
export function IsPartyId(validationOptions?: ValidationOptions) {
  return function (object: object, propertyName: string) {
    registerDecorator({
      target: object.constructor,
      propertyName: propertyName,
      options: validationOptions,
      constraints: [],
      validator: IsPartyIdConstraint,
    });
  };
}
