// Mock data for SD Pelita Bangsa
const SCHOOL = { name: 'SD Pelita Bangsa', short: 'PB', tahunAjaran: '2025/2026' };

const KELAS = ['1A', '1B', '2A', '2B', '3A', '3B', '4A', '4B', '5A', '5B', '6A', '6B'];

const SISWA = [
  { id: 'S001', nama: 'Aisyah Putri Maharani', kelas: '4A', nis: '24001', wali: 'W001' },
  { id: 'S002', nama: 'Bagas Pratama Wijaya',  kelas: '4A', nis: '24002', wali: 'W002' },
  { id: 'S003', nama: 'Citra Lestari Anggraini',kelas: '4A', nis: '24003', wali: 'W003' },
  { id: 'S004', nama: 'Dimas Aryasatya Putra', kelas: '4A', nis: '24004', wali: 'W004' },
  { id: 'S005', nama: 'Elina Salsabila',        kelas: '4A', nis: '24005', wali: 'W005' },
  { id: 'S006', nama: 'Farhan Rizky Maulana',   kelas: '4A', nis: '24006', wali: 'W006' },
  { id: 'S007', nama: 'Gita Permata Sari',      kelas: '4A', nis: '24007', wali: 'W007' },
  { id: 'S008', nama: 'Hafizh Aditya Nugraha',  kelas: '4A', nis: '24008', wali: 'W008' },
  { id: 'S009', nama: 'Indira Cahaya Putri',    kelas: '4A', nis: '24009', wali: 'W009' },
  { id: 'S010', nama: 'Joko Aulia Rahman',      kelas: '4A', nis: '24010', wali: 'W010' },
  { id: 'S011', nama: 'Kayla Khairunnisa',      kelas: '4A', nis: '24011', wali: 'W011' },
  { id: 'S012', nama: 'Lukman Hidayatullah',    kelas: '4A', nis: '24012', wali: 'W012' },
  { id: 'S013', nama: 'Maya Anindita',          kelas: '4A', nis: '24013', wali: 'W013' },
  { id: 'S014', nama: 'Naufal Ibrahim',         kelas: '4A', nis: '24014', wali: 'W014' },
  // Class 4B (a few)
  { id: 'S101', nama: 'Olivia Permatasari',     kelas: '4B', nis: '24101', wali: 'W101' },
  { id: 'S102', nama: 'Putra Adi Nugroho',      kelas: '4B', nis: '24102', wali: 'W102' },
  { id: 'S103', nama: 'Qonita Salma',           kelas: '4B', nis: '24103', wali: 'W103' },
  // Two siblings of guardian W003
  { id: 'S301', nama: 'Citra Lestari Anggraini',kelas: '4A', nis: '24003', wali: 'W003' },
];
// dedupe (we already have S003 as Citra under W003); remove S301
const STUDENTS = SISWA.filter(s => s.id !== 'S301');

// Add Dimas's sister, child of W004 (Dimas's guardian) → so guardian view shows 2 kids
STUDENTS.push({ id: 'S015', nama: 'Dini Aryasatya', kelas: '2A', nis: '24205', wali: 'W004' });

const WALI = {
  W001: { id: 'W001', nama: 'Bpk. Hendra Maharani', hp: '0812-3456-7801' },
  W002: { id: 'W002', nama: 'Ibu Sari Wijaya',      hp: '0812-3456-7802' },
  W003: { id: 'W003', nama: 'Bpk. Anton Anggraini', hp: '0812-3456-7803' },
  W004: { id: 'W004', nama: 'Ibu Ratna Aryasatya',  hp: '0812-3456-7804' },
  W005: { id: 'W005', nama: 'Bpk. Salim Salsabila', hp: '0812-3456-7805' },
  W006: { id: 'W006', nama: 'Ibu Dewi Maulana',     hp: '0812-3456-7806' },
  W007: { id: 'W007', nama: 'Bpk. Eko Permata',     hp: '0812-3456-7807' },
  W008: { id: 'W008', nama: 'Ibu Lia Nugraha',      hp: '0812-3456-7808' },
  W009: { id: 'W009', nama: 'Bpk. Surya Cahaya',    hp: '0812-3456-7809' },
  W010: { id: 'W010', nama: 'Ibu Astrid Aulia',     hp: '0812-3456-7810' },
  W011: { id: 'W011', nama: 'Bpk. Hadi Khairunnisa',hp: '0812-3456-7811' },
  W012: { id: 'W012', nama: 'Ibu Zahra Hidayat',    hp: '0812-3456-7812' },
  W013: { id: 'W013', nama: 'Bpk. Tomi Anindita',   hp: '0812-3456-7813' },
  W014: { id: 'W014', nama: 'Ibu Nurul Ibrahim',    hp: '0812-3456-7814' },
  W101: { id: 'W101', nama: 'Bpk. Bambang Permatasari', hp: '0812-3456-7901' },
  W102: { id: 'W102', nama: 'Ibu Yanti Adi',        hp: '0812-3456-7902' },
  W103: { id: 'W103', nama: 'Bpk. Iwan Salma',      hp: '0812-3456-7903' },
};

// Bill types
const BILL_TYPES = {
  spp:        { label: 'SPP Bulanan',    icon: 'calendar' },
  daftar:     { label: 'Uang Pendaftaran', icon: 'edit' },
  seragam:    { label: 'Seragam',        icon: 'shirt' },
  buku:       { label: 'Buku Paket',     icon: 'book' },
  kegiatan:   { label: 'Kegiatan/Wisata',icon: 'map' },
  ujian:      { label: 'Biaya Ujian',    icon: 'clipboard' },
};

// Tagihan (bills) — these are the master bill definitions Finance creates.
// Each bill produces one "tagihan_siswa" row per applicable student.
const TAGIHAN = [
  { id: 'T001', tipe: 'spp',      nama: 'SPP Oktober 2025',     nominal: 350000, jatuhTempo: '2025-10-10', target: 'all',  kelas: null },
  { id: 'T002', tipe: 'spp',      nama: 'SPP November 2025',    nominal: 350000, jatuhTempo: '2025-11-10', target: 'all',  kelas: null },
  { id: 'T003', tipe: 'spp',      nama: 'SPP Desember 2025',    nominal: 350000, jatuhTempo: '2025-12-10', target: 'all',  kelas: null },
  { id: 'T004', tipe: 'spp',      nama: 'SPP Januari 2026',     nominal: 350000, jatuhTempo: '2026-01-10', target: 'all',  kelas: null },
  { id: 'T005', tipe: 'spp',      nama: 'SPP Februari 2026',    nominal: 350000, jatuhTempo: '2026-02-10', target: 'all',  kelas: null },
  { id: 'T006', tipe: 'spp',      nama: 'SPP Maret 2026',       nominal: 350000, jatuhTempo: '2026-03-10', target: 'all',  kelas: null },
  { id: 'T007', tipe: 'spp',      nama: 'SPP April 2026',       nominal: 350000, jatuhTempo: '2026-04-10', target: 'all',  kelas: null },
  { id: 'T008', tipe: 'spp',      nama: 'SPP Mei 2026',         nominal: 350000, jatuhTempo: '2026-05-10', target: 'all',  kelas: null },
  { id: 'T010', tipe: 'kegiatan', nama: 'Wisata Edukasi Bandung',nominal: 425000, jatuhTempo: '2026-04-25', target: 'kelas',kelas: ['4A','4B','5A','5B'] },
  { id: 'T011', tipe: 'ujian',    nama: 'Ujian Tengah Semester', nominal: 150000, jatuhTempo: '2026-03-20', target: 'all',  kelas: null },
  { id: 'T012', tipe: 'buku',     nama: 'Buku Paket Smt 2',     nominal: 285000, jatuhTempo: '2026-01-25', target: 'all',  kelas: null },
];

// Generate tagihan_siswa: per-student rows. Pre-seed payment status realistically.
// Status: 'lunas' | 'belum' | 'jatuh_tempo' (overdue)
function generateLedger() {
  const today = new Date('2026-05-05');
  const rows = [];
  let cnt = 1;
  for (const t of TAGIHAN) {
    const applicableStudents = STUDENTS.filter(s => {
      if (t.target === 'all') return true;
      if (t.target === 'kelas') return t.kelas.includes(s.kelas);
      return false;
    });
    for (const s of applicableStudents) {
      const due = new Date(t.jatuhTempo);
      const isOverdue = due < today;
      // Seed: most past bills are paid (~85%), some are overdue. Future bills are belum.
      let status, paidAt = null, paidBy = null, receiptNo = null;
      const r = hash(`${t.id}-${s.id}`);
      if (due < today) {
        if (r % 100 < 86) {
          status = 'lunas';
          // paid 0–8 days after due
          const offset = (r % 9);
          const d = new Date(due); d.setDate(d.getDate() + offset);
          paidAt = d.toISOString().slice(0, 10);
          paidBy = ['Finance — Ibu Sinta', 'Finance — Bpk. Roni'][r % 2];
          receiptNo = `RCP-${String(cnt).padStart(5, '0')}`;
        } else {
          status = 'jatuh_tempo';
        }
      } else {
        status = 'belum';
      }
      rows.push({
        id: `L${String(cnt++).padStart(4, '0')}`,
        tagihanId: t.id,
        siswaId: s.id,
        nominal: t.nominal,
        jatuhTempo: t.jatuhTempo,
        status, paidAt, paidBy, receiptNo,
      });
    }
  }
  return rows;
}

function hash(s) {
  let h = 0;
  for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) >>> 0;
  return h;
}

const LEDGER = generateLedger();

// Helpers
function formatRupiah(n) {
  if (n == null) return '—';
  return 'Rp ' + Math.round(n).toLocaleString('id-ID');
}
function formatRupiahShort(n) {
  if (n == null) return '—';
  if (n >= 1_000_000) return 'Rp ' + (n / 1_000_000).toFixed(n % 1_000_000 === 0 ? 0 : 1) + 'jt';
  if (n >= 1_000) return 'Rp ' + (n / 1_000).toFixed(0) + 'rb';
  return 'Rp ' + n;
}
function formatDate(iso) {
  if (!iso) return '—';
  const d = new Date(iso);
  const months = ['Jan','Feb','Mar','Apr','Mei','Jun','Jul','Agu','Sep','Okt','Nov','Des'];
  return `${d.getDate()} ${months[d.getMonth()]} ${d.getFullYear()}`;
}
function formatDateLong(iso) {
  if (!iso) return '—';
  const d = new Date(iso);
  const months = ['Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember'];
  const days = ['Minggu','Senin','Selasa','Rabu','Kamis','Jumat','Sabtu'];
  return `${days[d.getDay()]}, ${d.getDate()} ${months[d.getMonth()]} ${d.getFullYear()}`;
}
function avatarColor(seed) {
  const palette = [
    'oklch(0.62 0.14 50)',  // amber
    'oklch(0.58 0.13 200)', // teal
    'oklch(0.55 0.14 280)', // indigo
    'oklch(0.55 0.14 145)', // green
    'oklch(0.6 0.14 0)',    // rose
    'oklch(0.58 0.14 320)', // pink
    'oklch(0.55 0.13 80)',  // olive
  ];
  return palette[hash(seed) % palette.length];
}
function initials(nama) {
  return nama.split(' ').slice(0, 2).map(w => w[0]).join('').toUpperCase();
}
function tagihanStatus(row, today = new Date('2026-05-05')) {
  if (row.status === 'lunas') return 'lunas';
  const d = new Date(row.jatuhTempo);
  if (d < today) return 'jatuh_tempo';
  // upcoming within 7 days?
  const diffDays = (d - today) / (24 * 3600 * 1000);
  if (diffDays <= 7) return 'segera';
  return 'belum';
}

Object.assign(window, {
  SCHOOL, KELAS, STUDENTS, WALI, BILL_TYPES, TAGIHAN, LEDGER,
  formatRupiah, formatRupiahShort, formatDate, formatDateLong,
  avatarColor, initials, tagihanStatus, hash,
});
