// ReportData.jsx
// Shared sample data + helpers for the three Project Baseline report variants.

const FRACTION_CONCEPTS = [
  { id: 'cmp-frac',  label: 'Comparing common fractions',                short: 'Compare fractions',        segs: [12, 24, 36, 28] },
  { id: 'eq-prop',   label: 'Equivalence (proper fractions)',            short: 'Equivalence (proper)',     segs: [20, 28, 32, 20] },
  { id: 'eq-mixed',  label: 'Equivalence (mixed / improper)',            short: 'Equivalence (mixed)',      segs: [34, 30, 22, 14] },
  { id: 'rnd-dec',   label: 'Rounding decimals',                         short: 'Round decimals',           segs: [16, 24, 34, 26] },
  { id: 'cmp-dec',   label: 'Comparing decimals',                        short: 'Compare decimals',         segs: [18, 27, 35, 20] },
  { id: 'eq-fdp',    label: 'Equivalence (fractions, decimals, %)',      short: 'Equivalence (F/D/%)',      segs: [36, 32, 22, 10] },
  { id: 'add-dec',   label: 'Adding decimals',                           short: 'Add decimals',             segs: [10, 18, 38, 34] },
  { id: 'sub-dec',   label: 'Subtracting decimals',                      short: 'Subtract decimals',        segs: [12, 22, 36, 30] },
  { id: 'add-frac',  label: 'Adding fractions',                          short: 'Add fractions',            segs: [30, 32, 24, 14] },
  { id: 'sub-frac',  label: 'Subtracting fractions',                     short: 'Subtract fractions',       segs: [32, 30, 24, 14] },
  { id: 'int-frac',  label: 'Interpreting & using fractions',            short: 'Use fractions',            segs: [28, 30, 28, 14] },
  { id: 'int-pct',   label: 'Interpreting & using percentages',          short: 'Use percentages',          segs: [42, 30, 18, 10] },
  { id: 'int-ratio', label: 'Interpreting & using ratio & proportion',   short: 'Use ratio & proportion',   segs: [48, 28, 16,  8] },
];

const BAND_LABELS = ['Building (below Gr 3)', 'Developing (Gr 3 to 5)', 'Securing (Gr 6 to 7)', 'Ready for Gr 8'];
const BAND_COLORS = ['var(--band-below)', 'var(--band-low)', 'var(--band-near)', 'var(--band-at)'];
const BAND_KEYS   = ['below', 'low', 'near', 'at'];

// Compute KPIs from the concept data
function computeKpis() {
  let totalSegs = [0,0,0,0];
  FRACTION_CONCEPTS.forEach(c => c.segs.forEach((v, i) => totalSegs[i] += v));
  const N = FRACTION_CONCEPTS.length;
  totalSegs = totalSegs.map(v => Math.round(v / N));
  return {
    classAvg: totalSegs[2] + totalSegs[3], // at / near grade level
    atRisk: totalSegs[0] + totalSegs[1],
    strongest: FRACTION_CONCEPTS.slice().sort((a,b) => (b.segs[2]+b.segs[3]) - (a.segs[2]+a.segs[3]))[0],
    weakest:   FRACTION_CONCEPTS.slice().sort((a,b) => (a.segs[2]+a.segs[3]) - (b.segs[2]+b.segs[3]))[0],
    bands: totalSegs,
  };
}

// Sample roster. 28 Grade 7 learners. lvls[] mirrors concept order; values 0..3 band index.
function generateRoster() {
  const names = [
    'Amahle Nkomo', 'Bongani Khumalo', 'Chloe van Rensburg', 'Daniel Pretorius',
    'Esihle Mthembu', 'Faatimah Patel', 'Gugu Sibiya', 'Hannah Steyn',
    'Iman Adams', 'Jacob de Wet', 'Kagiso Maluleke', 'Lerato Dlamini',
    'Mia van der Merwe', 'Nathan Booysen', 'Owethu Mahlangu', 'Pranav Naidoo',
    'Qiniso Buthelezi', 'Rachel Coetzee', 'Sipho Mabaso', 'Thabo Molefe',
    'Uthando Zulu', 'Veer Reddy', 'Wandile Nene', 'Xolani Tshabalala',
    'Yusuf Cassiem', 'Zinhle Mokoena', 'Adam Joubert', 'Busisiwe Khoza',
  ];
  // Deterministic pseudo-random
  function rand(i, j) {
    const x = Math.sin((i + 1) * 131 + (j + 1) * 47) * 10000;
    return x - Math.floor(x);
  }
  return names.map((name, i) => {
    const lvls = FRACTION_CONCEPTS.map((c, j) => {
      const r = rand(i, j);
      // weight by concept difficulty
      const w = c.segs;
      let cum = 0;
      const total = w.reduce((a,b)=>a+b,0);
      for (let k = 0; k < 4; k++) {
        cum += w[k] / total;
        if (r < cum) return k;
      }
      return 3;
    });
    const avg = lvls.reduce((a,b)=>a+b,0) / lvls.length;
    const masteredCount = lvls.filter(l => l >= 2).length;
    return { name, lvls, avg, masteredCount, gradeLevel: avg < 0.8 ? 'Gr 2-3' : avg < 1.5 ? 'Gr 3-5' : avg < 2.3 ? 'Gr 6-7' : 'Gr 7+' };
  });
}

function maxBandSeg(c) {
  let max = 0, idx = 0;
  c.segs.forEach((v, i) => { if (v > max) { max = v; idx = i; }});
  return idx;
}

Object.assign(window, { FRACTION_CONCEPTS, BAND_LABELS, BAND_COLORS, BAND_KEYS, computeKpis, generateRoster, maxBandSeg });
