<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return redirect()->route('login');
});

Auth::routes();

Route::get('/logout', function () {
    Auth::logout();
    request()->session()->invalidate();
    request()->session()->regenerateToken();
    return redirect('/');
})->name('logout.get');

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

// Profil pengguna (semua role)
Route::middleware('auth')->group(function () {
    Route::get('/profile', [App\Http\Controllers\ProfileController::class, 'show'])->name('profile');
    Route::put('/profile', [App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile/foto', [App\Http\Controllers\ProfileController::class, 'hapusFoto'])->name('profile.hapus-foto');
});

// Checker Routes
Route::prefix('checker')->name('checker.')->middleware(['auth', 'role:checker'])->group(function () {
    Route::get('/dashboard', [App\Http\Controllers\Checker\DashboardController::class, 'index'])->name('dashboard');
    Route::get('/pengesahan', [App\Http\Controllers\Checker\PengesahanController::class, 'index'])->name('pengesahan.index');
    Route::get('/pengesahan/{pengesahan}', [App\Http\Controllers\Checker\PengesahanController::class, 'show'])->name('pengesahan.show');
    Route::put('/pengesahan/{pengesahan}/status', [App\Http\Controllers\Checker\PengesahanController::class, 'updateStatus'])->name('pengesahan.updateStatus');
    Route::get('/sejarah', [App\Http\Controllers\Checker\PengesahanController::class, 'sejarah'])->name('sejarah');
});

// Supervisor Routes
Route::prefix('supervisor')->name('supervisor.')->middleware(['auth', 'role:supervisor'])->group(function () {
    Route::get('/dashboard', [App\Http\Controllers\Supervisor\DashboardController::class, 'index'])->name('dashboard');
    Route::get('/senarai', [App\Http\Controllers\Supervisor\DashboardController::class, 'senarai'])->name('senarai');
    Route::get('/lihat-buku/{user}', [App\Http\Controllers\Supervisor\DashboardController::class, 'lihatBuku'])->name('lihat-buku');
    Route::get('/lihat-buku-cetak/{user}', [App\Http\Controllers\Supervisor\DashboardController::class, 'lihatBukuCetak'])->name('lihat-buku-cetak');
    Route::get('/laporan/buku', [App\Http\Controllers\Supervisor\LaporanController::class, 'buku'])->name('laporan.buku');
    Route::get('/laporan/buku-cetak', [App\Http\Controllers\Supervisor\LaporanController::class, 'bukuCetak'])->name('laporan.buku-cetak');
    Route::get('/laporan/buku-detail', [App\Http\Controllers\Supervisor\LaporanController::class, 'bukuDetail'])->name('laporan.buku-detail');
    Route::get('/laporan/senarai-sekolah', [App\Http\Controllers\Supervisor\LaporanController::class, 'senaraiSekolah'])->name('laporan.senarai-sekolah');
    Route::get('/laporan/pengesahan', [App\Http\Controllers\Supervisor\LaporanController::class, 'pengesahan'])->name('laporan.pengesahan');
    Route::get('/laporan/pengesahan-cetak', [App\Http\Controllers\Supervisor\LaporanController::class, 'pengesahanCetak'])->name('laporan.pengesahan-cetak');
    Route::get('/laporan/pengesahan-detail', [App\Http\Controllers\Supervisor\LaporanController::class, 'pengesahanDetail'])->name('laporan.pengesahan-detail');
    Route::get('/laporan/senarai-sekolah-lebihan', [App\Http\Controllers\Supervisor\LaporanController::class, 'senaraiSekolahLebihan'])->name('laporan.senarai-sekolah-lebihan');
    Route::get('/laporan/buku-rosak', [App\Http\Controllers\Supervisor\LaporanController::class, 'bukuRosak'])->name('laporan.buku-rosak');
    Route::get('/laporan/buku-rosak-cetak', [App\Http\Controllers\Supervisor\LaporanController::class, 'bukuRosakCetak'])->name('laporan.buku-rosak-cetak');
    Route::get('/laporan/senarai-sekolah-rosak', [App\Http\Controllers\Supervisor\LaporanController::class, 'senaraiSekolahRosak'])->name('laporan.senarai-sekolah-rosak');
    Route::get('/laporan/buku-rosak-detail/{user}', [App\Http\Controllers\Supervisor\LaporanController::class, 'bukuRosakDetail'])->name('laporan.buku-rosak-detail');
    Route::get('/laporan/kkm-buku', [App\Http\Controllers\Supervisor\LaporanController::class, 'kkmBuku'])->name('laporan.kkm-buku');
    Route::get('/laporan/kkm-buku-cetak', [App\Http\Controllers\Supervisor\LaporanController::class, 'kkmBukuCetak'])->name('laporan.kkm-buku-cetak');
    Route::get('/laporan/kkm-senarai-sekolah', [App\Http\Controllers\Supervisor\LaporanController::class, 'kkmSenaraiSekolah'])->name('laporan.kkm-senarai-sekolah');
    Route::get('/laporan/kkm-lebih-buku', [App\Http\Controllers\Supervisor\LaporanController::class, 'kkmLebihBuku'])->name('laporan.kkm-lebih-buku');
    Route::get('/laporan/kkm-lebih-buku-cetak', [App\Http\Controllers\Supervisor\LaporanController::class, 'kkmLebihBukuCetak'])->name('laporan.kkm-lebih-buku-cetak');
    Route::get('/laporan/kkm-senarai-sekolah-lebihan', [App\Http\Controllers\Supervisor\LaporanController::class, 'kkmSenaraiSekolahLebihan'])->name('laporan.kkm-senarai-sekolah-lebihan');
    Route::get('/laporan/kkm-buku-rosak', [App\Http\Controllers\Supervisor\LaporanController::class, 'kkmBukuRosak'])->name('laporan.kkm-buku-rosak');
    Route::get('/laporan/kkm-buku-rosak-cetak', [App\Http\Controllers\Supervisor\LaporanController::class, 'kkmBukuRosakCetak'])->name('laporan.kkm-buku-rosak-cetak');
    Route::get('/laporan/kkm-senarai-sekolah-rosak', [App\Http\Controllers\Supervisor\LaporanController::class, 'kkmSenaraiSekolahRosak'])->name('laporan.kkm-senarai-sekolah-rosak');

    // Tetapan kebenaran edit semula
    Route::get('/tetapan', [App\Http\Controllers\Supervisor\TetapanController::class, 'index'])->name('tetapan.index');
    Route::post('/tetapan', [App\Http\Controllers\Supervisor\TetapanController::class, 'store'])->name('tetapan.store');
    Route::delete('/tetapan/{pengesahan}', [App\Http\Controllers\Supervisor\TetapanController::class, 'revoke'])->name('tetapan.revoke');
});

// Pemohon Routes
Route::prefix('pemohon')->name('pemohon.')->middleware(['auth', 'role:pemohon'])->group(function () {
    Route::get('/dashboard', [App\Http\Controllers\Pemohon\DashboardController::class, 'index'])->name('dashboard');
    Route::get('/profile', [App\Http\Controllers\Pemohon\ProfileController::class, 'show'])->name('profile');
    Route::put('/profile', [App\Http\Controllers\Pemohon\ProfileController::class, 'update'])->name('profile.update');
    Route::get('/jumlah-buku', [App\Http\Controllers\Pemohon\JumlahBukuController::class, 'index'])->name('jumlah-buku.index');
    Route::post('/jumlah-buku', [App\Http\Controllers\Pemohon\JumlahBukuController::class, 'store'])->name('jumlah-buku.store');
    Route::get('/pengesahan', [App\Http\Controllers\Pemohon\PengesahanController::class, 'index'])->name('pengesahan.index');
    Route::post('/pengesahan', [App\Http\Controllers\Pemohon\PengesahanController::class, 'hantar'])->name('pengesahan.hantar');
    Route::get('/pengesahan/status', [App\Http\Controllers\Pemohon\PengesahanController::class, 'status'])->name('pengesahan.status');
    Route::get('/pengesahan/{pengesahan}/cetak', [App\Http\Controllers\Pemohon\PengesahanController::class, 'cetak'])->name('pengesahan.cetak');
});

// Admin Routes
Route::prefix('admin')->name('admin.')->middleware('auth')->group(function () {
    Route::get('/dashboard', [App\Http\Controllers\HomeController::class, 'index'])->name('dashboard');

    // Pengguna
    Route::get('/pengguna', [App\Http\Controllers\Admin\UserController::class, 'index'])->name('users.index');
    Route::get('/pengguna/daftar', [App\Http\Controllers\Admin\UserController::class, 'create'])->name('users.create');
    Route::post('/pengguna', [App\Http\Controllers\Admin\UserController::class, 'store'])->name('users.store');
    Route::get('/pengguna/{user}/edit', [App\Http\Controllers\Admin\UserController::class, 'edit'])->name('users.edit');
    Route::put('/pengguna/{user}', [App\Http\Controllers\Admin\UserController::class, 'update'])->name('users.update');
    Route::delete('/pengguna/{user}', [App\Http\Controllers\Admin\UserController::class, 'destroy'])->name('users.destroy');
    Route::post('/pengguna/import-pukal', [App\Http\Controllers\Admin\UserController::class, 'importPukal'])->name('users.import-pukal');
    Route::get('/pengguna/template-excel', [App\Http\Controllers\Admin\UserController::class, 'templateExcel'])->name('users.template-excel');

    // Jenis Sekolah
    Route::get('/jenis-sekolah', [App\Http\Controllers\Admin\JenisSekolahController::class, 'index'])->name('jenis-sekolah.index');
    Route::post('/jenis-sekolah', [App\Http\Controllers\Admin\JenisSekolahController::class, 'store'])->name('jenis-sekolah.store');
    Route::put('/jenis-sekolah/{jenisSekolah}', [App\Http\Controllers\Admin\JenisSekolahController::class, 'update'])->name('jenis-sekolah.update');
    Route::delete('/jenis-sekolah/{jenisSekolah}', [App\Http\Controllers\Admin\JenisSekolahController::class, 'destroy'])->name('jenis-sekolah.destroy');

    // Darjah
    Route::get('/darjah', [App\Http\Controllers\Admin\DarjahController::class, 'index'])->name('darjah.index');
    Route::post('/darjah', [App\Http\Controllers\Admin\DarjahController::class, 'store'])->name('darjah.store');
    Route::put('/darjah/{darjah}', [App\Http\Controllers\Admin\DarjahController::class, 'update'])->name('darjah.update');
    Route::delete('/darjah/{darjah}', [App\Http\Controllers\Admin\DarjahController::class, 'destroy'])->name('darjah.destroy');

    // Iklan
    Route::get('/iklan', [App\Http\Controllers\Admin\IklanController::class, 'index'])->name('iklan.index');
    Route::post('/iklan', [App\Http\Controllers\Admin\IklanController::class, 'store'])->name('iklan.store');
    Route::put('/iklan/{iklan}', [App\Http\Controllers\Admin\IklanController::class, 'update'])->name('iklan.update');
    Route::delete('/iklan/{iklan}', [App\Http\Controllers\Admin\IklanController::class, 'destroy'])->name('iklan.destroy');

    // Background Login
    Route::get('/back-login', [App\Http\Controllers\Admin\BackLoginController::class, 'index'])->name('back-login.index');
    Route::post('/back-login', [App\Http\Controllers\Admin\BackLoginController::class, 'store'])->name('back-login.store');
    Route::delete('/back-login', [App\Http\Controllers\Admin\BackLoginController::class, 'destroy'])->name('back-login.destroy');
    Route::delete('/back-login/{filename}', [App\Http\Controllers\Admin\BackLoginController::class, 'destroyOne'])->name('back-login.destroy-one');
    Route::post('/back-login/{filename}/caption', [App\Http\Controllers\Admin\BackLoginController::class, 'updateCaption'])->name('back-login.update-caption');

    // Bantuan
    Route::get('/bantuan', [App\Http\Controllers\Admin\BantuanController::class, 'index'])->name('bantuan.index');
    Route::post('/bantuan', [App\Http\Controllers\Admin\BantuanController::class, 'store'])->name('bantuan.store');
    Route::put('/bantuan/{bantuan}', [App\Http\Controllers\Admin\BantuanController::class, 'update'])->name('bantuan.update');
    Route::delete('/bantuan/{bantuan}', [App\Http\Controllers\Admin\BantuanController::class, 'destroy'])->name('bantuan.destroy');

    // Buku
    Route::get('/buku', [App\Http\Controllers\Admin\BukuController::class, 'index'])->name('buku.index');
    Route::post('/buku', [App\Http\Controllers\Admin\BukuController::class, 'store'])->name('buku.store');
    Route::put('/buku/{buku}', [App\Http\Controllers\Admin\BukuController::class, 'update'])->name('buku.update');
    Route::delete('/buku/{buku}', [App\Http\Controllers\Admin\BukuController::class, 'destroy'])->name('buku.destroy');
    Route::post('/buku/import-pukal', [App\Http\Controllers\Admin\BukuController::class, 'importPukal'])->name('buku.import-pukal');
    Route::get('/buku/template-excel', [App\Http\Controllers\Admin\BukuController::class, 'templateExcel'])->name('buku.template-excel');

    // Laporan
    Route::get('/laporan/pesan-buku', [App\Http\Controllers\Admin\LaporanController::class, 'pesanBuku'])->name('laporan.pesan-buku');
    Route::get('/laporan/pesan-buku-detail', [App\Http\Controllers\Admin\LaporanController::class, 'pesanBukuDetail'])->name('laporan.pesan-buku-detail');
    Route::get('/laporan/pesan-buku-cetak', [App\Http\Controllers\Admin\LaporanController::class, 'pesanBukuCetak'])->name('laporan.pesan-buku-cetak');
    Route::get('/laporan/lebih-buku', [App\Http\Controllers\Admin\LaporanController::class, 'lebihBuku'])->name('laporan.lebih-buku');
    Route::get('/laporan/lebih-buku-detail', [App\Http\Controllers\Admin\LaporanController::class, 'lebihBukuDetail'])->name('laporan.lebih-buku-detail');
    Route::get('/laporan/lebih-buku-cetak', [App\Http\Controllers\Admin\LaporanController::class, 'lebihBukuCetak'])->name('laporan.lebih-buku-cetak');

    // Laporan KKM
    Route::get('/laporan/buku-rosak', [App\Http\Controllers\Admin\LaporanController::class, 'bukuRosak'])->name('laporan.buku-rosak');
    Route::get('/laporan/buku-rosak-cetak', [App\Http\Controllers\Admin\LaporanController::class, 'bukuRosakCetak'])->name('laporan.buku-rosak-cetak');

    Route::get('/laporan/kkm-pesan-buku', [App\Http\Controllers\Admin\LaporanController::class, 'kkmPesanBuku'])->name('laporan.kkm-pesan-buku');
    Route::get('/laporan/kkm-pesan-buku-cetak', [App\Http\Controllers\Admin\LaporanController::class, 'kkmPesanBukuCetak'])->name('laporan.kkm-pesan-buku-cetak');
    Route::get('/laporan/kkm-lebih-buku', [App\Http\Controllers\Admin\LaporanController::class, 'kkmLebihBuku'])->name('laporan.kkm-lebih-buku');
    Route::get('/laporan/kkm-lebih-buku-cetak', [App\Http\Controllers\Admin\LaporanController::class, 'kkmLebihBukuCetak'])->name('laporan.kkm-lebih-buku-cetak');
    Route::get('/laporan/kkm-buku-rosak', [App\Http\Controllers\Admin\LaporanController::class, 'kkmBukuRosak'])->name('laporan.kkm-buku-rosak');
    Route::get('/laporan/kkm-buku-rosak-cetak', [App\Http\Controllers\Admin\LaporanController::class, 'kkmBukuRosakCetak'])->name('laporan.kkm-buku-rosak-cetak');
});
