class TextProcessor {
  /**
   * Normalize Dubai area names to match database values
   */
  static normalizeAreaName(areaName) {
    const areaMapping = {
      'business bay': 'business bay',
      'downtown': 'burj khalifa',
      'downtown dubai': 'burj khalifa',
      'marina': 'dubai marina',
      'dubai marina': 'dubai marina',
      'jbr': 'jbr',
      'difc': 'difc',
      'jlt': 'jlt',
      'palm jumeirah': 'palm jumeirah',
      'arabian ranches': 'arabian ranches'
    };
    
    const normalized = areaName.toLowerCase().trim();
    return areaMapping[normalized] || normalized;
  }

  /**
   * Format currency for display
   */
  static formatCurrency(amount, currency = 'AED') {
    return new Intl.NumberFormat('en-AE', {
      style: 'currency',
      currency: currency,
      minimumFractionDigits: 0,
      maximumFractionDigits: 0
    }).format(amount);
  }

  /**
   * Format number with commas
   */
  static formatNumber(number) {
    return new Intl.NumberFormat('en-AE').format(number);
  }

  /**
   * Calculate percentage change
   */
  static calculatePercentageChange(oldValue, newValue) {
    if (!oldValue || oldValue === 0) return 0;
    return ((newValue - oldValue) / oldValue) * 100;
  }

  /**
   * Truncate text to specified length
   */
  static truncate(text, maxLength = 50) {
    if (!text || text.length <= maxLength) return text;
    return text.substring(0, maxLength) + '...';
  }

  /**
   * Clean and sanitize user input
   */
  static sanitizeInput(input) {
    if (!input) return '';
    return input.trim().replace(/[<>]/g, '');
  }

  /**
   * Extract numbers from text
   */
  static extractNumbers(text) {
    return text.match(/\d+/g) || [];
  }

  /**
   * Extract property type from text
   */
  static extractPropertyType(text) {
    const propertyTypes = {
      'apartment': ['apartment', 'flat', 'unit'],
      'villa': ['villa', 'house', 'townhouse'],
      'commercial': ['commercial', 'office', 'retail', 'shop'],
      'land': ['land', 'plot', 'parcel']
    };

    const lowerText = text.toLowerCase();
    
    for (const [type, keywords] of Object.entries(propertyTypes)) {
      if (keywords.some(keyword => lowerText.includes(keyword))) {
        return type;
      }
    }

    return null;
  }
}

module.exports = TextProcessor;

