import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common';

/**
 * Validates that a string is a valid CUID
 * CUID format: starts with 'c' followed by 24 alphanumeric characters
 */
@Injectable()
export class ParseCuidPipe implements PipeTransform<string, string> {
  transform(value: string): string {
    // CUID pattern: c followed by 24 alphanumeric characters
    const cuidRegex = /^c[a-z0-9]{24}$/;

    if (!cuidRegex.test(value)) {
      throw new BadRequestException(`Invalid ID format: ${value}`);
    }

    return value;
  }
}
