export const WO_FEEDBACK_RATING_OPTIONS = [
    'Excellent / ممتاز',
    'Good / جيد',
    'Average / متوسط',
    'Poor / ضعيف',
  ];
  
  /** Fixed dashboard order: Excellent → Good → Average → Poor */
  export function getWoFeedbackRatingRank(label: string): number {
    const l = label.toLowerCase();
    if (l.includes('excellent') || l.includes('ممتاز')) return 0;
    if (l.includes('good') || l.includes('جيد')) return 1;
    if (l.includes('average') || l.includes('متوسط')) return 2;
    if (l.includes('poor') || l.includes('ضعيف')) return 3;
    return -1;
  }
  
  export function getWoFeedbackRatingColor(label: string): string {
    const rank = getWoFeedbackRatingRank(label);
    if (rank === 0) return '#10B981';
    if (rank === 1) return '#3B82F6';
    if (rank === 2) return '#F59E0B';
    if (rank === 3) return '#EF4444';
    return '#6B7280';
  }
  
  /** Always return all four ratings in fixed order, filling missing counts with 0. */
  export function orderWoFeedbackChartData(data: any) {
    const apiLabels: string[] = data?.labels || [];
    const apiValues: number[] = data?.datasets?.[0]?.values || [];
    const byRank = new Map<number, { label: string; value: number }>();
  
    apiLabels.forEach((label: string, i: number) => {
      const rank = getWoFeedbackRatingRank(label);
      if (rank < 0) return;
      const value = Number(apiValues[i]) || 0;
      const existing = byRank.get(rank);
      if (existing) existing.value += value;
      else byRank.set(rank, { label, value });
    });
  
    const labels = WO_FEEDBACK_RATING_OPTIONS.map((canonical, rank) => byRank.get(rank)?.label ?? canonical);
    const values = WO_FEEDBACK_RATING_OPTIONS.map((_, rank) => byRank.get(rank)?.value ?? 0);
  
    return {
      ...(data || {}),
      labels,
      datasets: [{
        ...(data?.datasets?.[0] || { name: 'Work Orders' }),
        values,
      }],
    };
  }
  
  export function applyWoFeedbackChartColors(data: any) {
    const ordered = orderWoFeedbackChartData(data);
    const colors = ordered.labels.map((label: string) => getWoFeedbackRatingColor(label));
    return {
      ...ordered,
      type: 'Pie',
      datasets: [{
        ...(ordered.datasets?.[0] || { name: 'Work Orders', values: [] }),
        colors,
        values: ordered.datasets?.[0]?.values || [],
      }],
    };
  }
  