// Comprehensive Demo data seed runner.
// Seeds:
//   - All role demo accounts (admin, principal, teacher, student, parent, bursar, hr, librarian)
//   - Users, staff records, parent-student relationships
//   - Academics, classes, subjects, courses, attendance, assignments, grades
//   - Finance, fee groups, student fees, payments, expenses, payroll, payslips
//   - Teacher tools (schemes, lesson plans), medical records, online exams
//   - Facilities, hostels, transport, inventory, library, internal messages, notices
//   - System settings
//
// Idempotent: uses INSERT OR IGNORE and foreign-key lookup safety so re-running is safe.

const { seedUsersAndStaff } = require('./users_and_staff');
const { seedAcademicsAndGrading } = require('./academics_and_grading');
const { seedFinanceAndPayroll } = require('./finance_and_payroll');
const { seedTeacherMedicalExams } = require('./teacher_medical_exams');
const { seedFacilitiesAndServices } = require('./facilities_and_services');

async function seedDemoData(db) {
  // 1. Core Users, Staff & Relationships
  await seedUsersAndStaff(db);

  try { await seedAcademicsAndGrading(db); } catch (e) { console.warn('[seed] academics seed warning:', e.message); }
  try { await seedFinanceAndPayroll(db); } catch (e) { console.warn('[seed] finance seed warning:', e.message); }
  try { await seedTeacherMedicalExams(db); } catch (e) { console.warn('[seed] teacher/medical seed warning:', e.message); }
  try { await seedFacilitiesAndServices(db); } catch (e) { console.warn('[seed] facilities seed warning:', e.message); }

  // Default system_settings rows
  db.prepare(`INSERT OR IGNORE INTO system_settings (key, value, description, category) VALUES (?, ?, ?, ?)`)
    .run('school_name', 'Africa Alert Demo School', 'Display name of the school', 'general');
  db.prepare(`INSERT OR IGNORE INTO system_settings (key, value, description, category) VALUES (?, ?, ?, ?)`)
    .run('term', 'Term 1', 'Current academic term', 'academic');
  db.prepare(`INSERT OR IGNORE INTO system_settings (key, value, description, category) VALUES (?, ?, ?, ?)`)
    .run('default_grading_scale', 'zimsec_olevel', 'Default grading scale for new exams', 'academic');

  console.log('[seed] full system demo data seeded successfully (idempotent)');
}

module.exports = { seedDemoData };
