<?php
/**
 * SIAKAD - Entry Point / Front Controller
 * Sistem Informasi Akademik Sekolah
 */

// Session security HARUS sebelum session_start()
ini_set('session.cookie_httponly', 1);
ini_set('session.use_only_cookies', 1);
ini_set('session.cookie_samesite', 'Strict');

session_start();
require_once __DIR__ . '/config/app.php';

// Ambil route dari parameter ?page=
$route = isset($_GET['page'])
    ? preg_replace('/[^a-z0-9_]/', '', strtolower($_GET['page']))
    : 'dashboard';

// Halaman publik (tidak perlu login)
$publicPages = ['login', 'logout'];

// Paksa login untuk semua halaman non-publik
if (!in_array($route, $publicPages)) {
    requireLogin();
}

// Load pengaturan sekolah untuk layout (sidebar, topbar, footer)
$pengaturan = [];
if (isLoggedIn()) {
    $db  = getDB();
    $res = $db->query("SELECT * FROM pengaturan LIMIT 1");
    if ($res) $pengaturan = $res->fetch_assoc() ?? [];
}

// ============================================================
// Router
// ============================================================
switch ($route) {

    // ---- AUTH ----
    case 'login':
        require_once __DIR__ . '/controllers/AuthController.php';
        (new AuthController())->login();
        break;

    case 'logout':
        require_once __DIR__ . '/controllers/AuthController.php';
        (new AuthController())->logout();
        break;

    // ---- DASHBOARD ----
    case 'dashboard':
        require_once __DIR__ . '/controllers/DashboardController.php';
        (new DashboardController())->index();
        break;

    // ---- SISWA ----
    case 'siswa':
        require_once __DIR__ . '/controllers/SiswaController.php';
        (new SiswaController())->index();
        break;
    case 'siswa_tambah':
        require_once __DIR__ . '/controllers/SiswaController.php';
        (new SiswaController())->tambah();
        break;
    case 'siswa_edit':
        require_once __DIR__ . '/controllers/SiswaController.php';
        (new SiswaController())->edit();
        break;
    case 'siswa_hapus':
        require_once __DIR__ . '/controllers/SiswaController.php';
        (new SiswaController())->hapus();
        break;
    case 'siswa_detail':
        require_once __DIR__ . '/controllers/SiswaController.php';
        (new SiswaController())->detail();
        break;
    case 'siswa_import':
        require_once __DIR__ . '/controllers/SiswaController.php';
        (new SiswaController())->import();
        break;
    case 'siswa_export':
        require_once __DIR__ . '/controllers/SiswaController.php';
        (new SiswaController())->export();
        break;
    case 'siswa_surat_pindah':
        require_once __DIR__ . '/controllers/SiswaController.php';
        (new SiswaController())->suratPindah();
        break;

    // ---- GURU ----
    case 'guru':
        require_once __DIR__ . '/controllers/GuruController.php';
        (new GuruController())->index();
        break;
    case 'guru_tambah':
        require_once __DIR__ . '/controllers/GuruController.php';
        (new GuruController())->tambah();
        break;
    case 'guru_edit':
        require_once __DIR__ . '/controllers/GuruController.php';
        (new GuruController())->edit();
        break;
    case 'guru_hapus':
        require_once __DIR__ . '/controllers/GuruController.php';
        (new GuruController())->hapus();
        break;
    case 'guru_detail':
        require_once __DIR__ . '/controllers/GuruController.php';
        (new GuruController())->detail();
        break;
    case 'guru_import':
        require_once __DIR__ . '/controllers/GuruController.php';
        (new GuruController())->import();
        break;
    case 'guru_export':
        require_once __DIR__ . '/controllers/GuruController.php';
        (new GuruController())->export();
        break;
    case 'guru_template':
        require_once __DIR__ . '/controllers/GuruController.php';
        (new GuruController())->templateDownload();
        break;

    // ---- KELAS ----
    case 'kelas':
        require_once __DIR__ . '/controllers/KelasController.php';
        (new KelasController())->index();
        break;
    case 'kelas_tambah':
        require_once __DIR__ . '/controllers/KelasController.php';
        (new KelasController())->tambah();
        break;
    case 'kelas_edit':
        require_once __DIR__ . '/controllers/KelasController.php';
        (new KelasController())->edit();
        break;
    case 'kelas_hapus':
        require_once __DIR__ . '/controllers/KelasController.php';
        (new KelasController())->hapus();
        break;
    case 'kelas_anggota':
        require_once __DIR__ . '/controllers/KelasController.php';
        (new KelasController())->anggota();
        break;

    // ---- MATA PELAJARAN ----
    case 'mapel':
        require_once __DIR__ . '/controllers/MapelController.php';
        (new MapelController())->index();
        break;
    case 'mapel_tambah':
        require_once __DIR__ . '/controllers/MapelController.php';
        (new MapelController())->tambah();
        break;
    case 'mapel_edit':
        require_once __DIR__ . '/controllers/MapelController.php';
        (new MapelController())->edit();
        break;
    case 'mapel_hapus':
        require_once __DIR__ . '/controllers/MapelController.php';
        (new MapelController())->hapus();
        break;

    // ---- NILAI ----
    case 'nilai':
        require_once __DIR__ . '/controllers/NilaiController.php';
        (new NilaiController())->index();
        break;
    case 'nilai_siswa_ajax':
        require_once __DIR__ . '/controllers/NilaiController.php';
        (new NilaiController())->getSiswaAjax();
        break;
    case 'nilai_input':
        require_once __DIR__ . '/controllers/NilaiController.php';
        (new NilaiController())->input();
        break;
    case 'nilai_rapor':
        require_once __DIR__ . '/controllers/NilaiController.php';
        (new NilaiController())->rapor();
        break;
    case 'nilai_import':
        require_once __DIR__ . '/controllers/NilaiController.php';
        (new NilaiController())->importNilai();
        break;
    case 'nilai_template':
        require_once __DIR__ . '/controllers/NilaiController.php';
        (new NilaiController())->templateDownload();
        break;
    case 'nilai_export':
        require_once __DIR__ . '/controllers/NilaiController.php';
        (new NilaiController())->export();
        break;
    case 'nilai_export_rekap':
        require_once __DIR__ . '/controllers/NilaiController.php';
        (new NilaiController())->exportRekap();
        break;

    // ---- PENCARIAN ----
    case 'pencarian':
        require_once __DIR__ . '/controllers/PencarianController.php';
        (new PencarianController())->index();
        break;

    // ---- PENGATURAN ----
    case 'pengaturan_sekolah':
        require_once __DIR__ . '/controllers/PengaturanController.php';
        (new PengaturanController())->sekolah();
        break;
    case 'pengaturan_sandi':
        require_once __DIR__ . '/controllers/PengaturanController.php';
        (new PengaturanController())->sandi();
        break;
    case 'pengaturan_backup':
        require_once __DIR__ . '/controllers/PengaturanController.php';
        (new PengaturanController())->backup();
        break;

    // ---- 404 ----
    default:
        http_response_code(404);
        $pageTitle = '404 - Halaman Tidak Ditemukan';
        include __DIR__ . '/views/layouts/header.php';
        echo '
        <div class="d-flex align-items-center justify-content-center min-vh-100">
          <div class="text-center p-4">
            <div style="font-size:5rem;line-height:1;">🔍</div>
            <h2 class="mt-3 fw-700">Halaman Tidak Ditemukan</h2>
            <p class="text-muted">Halaman yang Anda cari tidak tersedia.</p>
            <a href="' . BASE_URL . '/index.php?page=dashboard" class="btn btn-primary mt-2">
              <i class="bi bi-house me-1"></i> Kembali ke Dashboard
            </a>
          </div>
        </div>';
        include __DIR__ . '/views/layouts/footer.php';
        break;
}
