// Helpers
const fmt = {
  num: (n) => {
    if (n == null || isNaN(n)) return '—';
    if (Math.abs(n) >= 1e12) return (n/1e12).toFixed(1)+'T';
    if (Math.abs(n) >= 1e9) return (n/1e9).toFixed(1)+'B';
    if (Math.abs(n) >= 1e6) return (n/1e6).toFixed(1)+'M';
    if (Math.abs(n) >= 1e3) return (n/1e3).toFixed(1)+'K';
    return Math.round(n).toLocaleString();
  },
  num2: (n) => {
    if (n == null || isNaN(n)) return '—';
    if (Math.abs(n) >= 1e12) return (n/1e12).toFixed(2)+'T';
    if (Math.abs(n) >= 1e9) return (n/1e9).toFixed(2)+'B';
    if (Math.abs(n) >= 1e6) return (n/1e6).toFixed(2)+'M';
    if (Math.abs(n) >= 1e3) return (n/1e3).toFixed(2)+'K';
    return Math.round(n).toLocaleString();
  },
  k: (n) => {
    if (n == null || isNaN(n)) return '—';
    if (Math.abs(n) >= 1e9) return (n/1e9).toFixed(1)+'B';
    if (Math.abs(n) >= 1e6) return (n/1e6).toFixed(1)+'M';
    return (n/1e3).toFixed(1)+'K';
  },
  int: (n) => n == null ? '—' : Math.round(n).toLocaleString('en-US'),
  pct: (n, d=1) => n == null ? '—' : (n*100).toFixed(d)+'%',
  pctRaw: (n, d=1) => n == null ? '—' : n.toFixed(d)+'%',
  idr: (n) => {
    if (n == null || isNaN(n)) return '—';
    const sign = n < 0 ? '-' : '';
    const absN = Math.abs(n);
    if (absN >= 1e12) return sign + (absN/1e12).toFixed(1)+'T';
    if (absN >= 1e9)  return sign + (absN/1e9).toFixed(1)+'B';
    if (absN >= 1e6)  return sign + (absN/1e6).toFixed(1)+'M';
    if (absN >= 1e3)  return sign + (absN/1e3).toFixed(1)+'K';
    return sign + Math.round(absN).toLocaleString();
  },
  idr2: (n) => {
    if (n == null || isNaN(n)) return '—';
    const sign = n < 0 ? '-' : '';
    const absN = Math.abs(n);
    if (absN >= 1e12) return sign + (absN/1e12).toFixed(2)+'T';
    if (absN >= 1e9)  return sign + (absN/1e9).toFixed(2)+'B';
    if (absN >= 1e6)  return sign + (absN/1e6).toFixed(2)+'M';
    if (absN >= 1e3)  return sign + (absN/1e3).toFixed(2)+'K';
    return sign + Math.round(absN).toLocaleString();
  },
  delta: (n) => {
    if (n == null || isNaN(n)) return '—';
    const sign = n > 0 ? '+' : '';
    return sign + fmt.idr(n);
  },
  deltaPct: (n) => {
    if (n == null || isNaN(n)) return '—';
    const sign = n > 0 ? '+' : '';
    return sign + n.toFixed(1) + '%';
  },
  columnFormatter: (values, format) => {
    if (format === 'pct') {
      return (v) => fmt.pct(v, 1);
    }
    if (format === 'mins') {
      return (v) => v == null || isNaN(v) ? '—' : v.toFixed(1) + 'm';
    }
    if (format === 'days') {
      return (v) => v == null || isNaN(v) ? '—' : v.toFixed(1) + 'd';
    }
    // Only apply scale grouping to currency and large numeric formats
    if (format !== 'idr' && format !== 'num' && format !== 'int' && format !== 'k' && format !== 'count' && format !== 'dec1' && format !== 'dec2') {
      return null;
    }

    const validVals = (values || []).map(v => Number(v)).filter(v => v != null && !isNaN(v) && v !== 0);
    if (validVals.length === 0) return null;

    // Count buckets to determine dominant scale
    let counts = { T: 0, B: 0, M: 0, K: 0, raw: 0 };
    validVals.forEach(v => {
      const absV = Math.abs(v);
      if (absV >= 1e12) counts.T++;
      else if (absV >= 1e9) counts.B++;
      else if (absV >= 1e6) counts.M++;
      else if (absV >= 1e3) counts.K++;
      else counts.raw++;
    });

    let dominantScale = 'raw';
    let maxCount = -1;
    ['T', 'B', 'M', 'K', 'raw'].forEach(scale => {
      if (counts[scale] > maxCount) {
        maxCount = counts[scale];
        dominantScale = scale;
      }
    });

    return (v) => {
      if (v == null || isNaN(v)) return '—';
      if (v === 0) return '0';

      const sign = v < 0 ? '-' : '';
      const absV = Math.abs(v);

      let formattedVal = '';
      switch (dominantScale) {
        case 'T':
          formattedVal = (absV / 1e12).toFixed(1) + 'T';
          break;
        case 'B':
          formattedVal = (absV / 1e9).toFixed(1) + 'B';
          break;
        case 'M':
          formattedVal = (absV / 1e6).toFixed(1) + 'M';
          break;
        case 'K':
          formattedVal = (absV / 1e3).toFixed(1) + 'K';
          break;
        default:
          if (format === 'dec1') formattedVal = absV.toFixed(1);
          else if (format === 'dec2') formattedVal = absV.toFixed(2);
          else formattedVal = Math.round(absV).toLocaleString('en-US');
      }

      return sign + formattedVal;
    };
  }
};

// Color per segment
const SEG_COLORS = {
  // Green gradient — DS split
  "DS1 - Experience Players":     "#065f46",
  "DS2 - Prize-Oriented Players": "#047857",
  "Power Players":                "#064e3b",
  "Social Squad":                 "#10b981",
  "Holiday Heros":                "#34d399",
  // Yellow gradient
  "Casual":                       "#b45309",
  "Drifters":                     "#d97706",
  "Budget":                       "#f59e0b",
  "Dormant":                      "#fbbf24",
  // Red gradient
  "Heavy Hitters":                "#991b1b",
  "Prize Chasers":                "#ef4444",
  // Gray
  "Outlier":                      "#9ca3af",
  // Legacy (keep for backward compat)
  "Dedicated Squad":              "#047857",
  "Holiday Heroes":               "#34d399",
};

const SEG_PASTEL_COLORS = {
  "DS1 - Experience Players":     "#ecfdf5",
  "DS2 - Prize-Oriented Players": "#ecfdf5",
  "Power Players":                "#ecfdf5",
  "Social Squad":                 "#ecfdf5",
  "Holiday Heros":                "#ecfdf5",
  "Casual":                       "#fffbeb",
  "Drifters":                     "#fffbeb",
  "Budget":                       "#fffbeb",
  "Dormant":                      "#fffbeb",
  "Heavy Hitters":                "#fef2f2",
  "Prize Chasers":                "#fef2f2",
  "Outlier":                      "#f9fafb",
  "Dedicated Squad":              "#ecfdf5",
  "Holiday Heroes":               "#ecfdf5",
};

const TIER_COLORS = {
  vip: "#e63329",
  loyal: "#1a2a5e",
  casual: "#5a76c7",
  light: "#9aa3bf",
  edge: "#f5d616",
};

window.fmt = fmt;
window.SEG_COLORS = SEG_COLORS;
window.SEG_PASTEL_COLORS = SEG_PASTEL_COLORS;
window.TIER_COLORS = TIER_COLORS;
