import { RequestType } from '../enums';
import { REQUEST_NUMBER_PREFIX } from '../constants';

export class RequestNumberUtil {
  /**
   * Generate unique request number
   * Format: {PREFIX}-{YEAR}-{SEQUENCE}
   * Example: RL-2024-000001
   */
  static generate(requestType: RequestType, sequence: number): string {
    const prefix = REQUEST_NUMBER_PREFIX[requestType] || 'RQ';
    const year = new Date().getFullYear();
    const paddedSequence = sequence.toString().padStart(6, '0');
    return `${prefix}-${year}-${paddedSequence}`;
  }

  /**
   * Parse request number to extract components
   */
  static parse(requestNumber: string): {
    prefix: string;
    year: number;
    sequence: number;
  } | null {
    const match = requestNumber.match(/^([A-Z]+)-(\d{4})-(\d+)$/);
    if (!match) return null;

    return {
      prefix: match[1],
      year: parseInt(match[2], 10),
      sequence: parseInt(match[3], 10),
    };
  }

  /**
   * Validate request number format
   */
  static isValid(requestNumber: string): boolean {
    return /^[A-Z]+-\d{4}-\d{6}$/.test(requestNumber);
  }
}
