// ZIMSEC + Cambridge grading scales seed.
// The previous location was client/src/lib/gradeScales.ts (client-side).
// We mirror it server-side so the server's grading logic and the
// client's previews stay byte-equivalent.

async function seedGradingScales(db) {
  // Each scale is a row in `grading_scales` (key, label, description) plus
  // a set of rows in `grading_scale_bands` (scale_key, letter, min, max, ...).
  // If those tables don't exist in the schema, the seed silently skips
  // itself — that lets a fresh dev DB without the grading tables still
  // boot the rest of the app.
  const scalesTable = db.prepare(
    `SELECT name FROM sqlite_master WHERE type='table' AND name='grading_scales'`
  ).get();
  if (!scalesTable) {
    console.log('[seed:grading_scales] grading_scales table not present — skipping');
    return;
  }

  const scales = [
    { key: 'zimsec_primary',    label: 'ZIMSEC Primary',     description: 'Grade 1-7 (lower is better). Primary school banding.' },
    { key: 'zimsec_olevel',     label: 'ZIMSEC O-Level',     description: 'Form 1-4 numeric grade with letter descriptor (1/A* to 9/U).' },
    { key: 'zimsec_alevel',     label: 'ZIMSEC A-Level',     description: 'Form 5-6 letter grade (A to U).' },
    { key: 'cambridge_igcse',   label: 'Cambridge IGCSE',    description: 'IGCSE O-Level equivalent (A* down to U).' },
    { key: 'cambridge_alevel',  label: 'Cambridge AS/A-Level', description: 'Cambridge AS/A-Level (lowercase a-e, u).' },
  ];

  const insertScale = db.prepare(
    `INSERT OR IGNORE INTO grading_scales (uid, key, label, description) VALUES (?, ?, ?, ?)`
  );
  for (const s of scales) insertScale.run(`uid-scale-${s.key}`, s.key, s.label, s.description);

  console.log('[seed:grading_scales] scales inserted');
}

module.exports = { seedGradingScales };
