<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\KatalogController;
use App\Http\Controllers\PeminjamanController;
use App\Http\Controllers\BacaOnlineController;
use App\Http\Controllers\RiwayatController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\BookController as AdminBookController;
use App\Http\Controllers\Admin\PengumumanController;
use App\Http\Controllers\Admin\UserController as AdminUserController;
use App\Http\Controllers\Guru\LaporanController;

Route::get('/', function () {
    return view('welcome');
});

// --- RUTE UNTUK PENGGUNA TERAUTENTIKASI (SISWA & PENJAGA PERPUS) ---
Route::middleware(['session.auth'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/katalog', [KatalogController::class, 'index'])->name('katalog.index');

    Route::prefix('peminjaman-offline')->name('peminjaman.')->group(function () {
        Route::get('/', [PeminjamanController::class, 'index'])->name('index');
        Route::get('/{id}/ringkasan', [PeminjamanController::class, 'ringkasan'])->name('ringkasan');
        Route::get('/{id}/form', [PeminjamanController::class, 'form'])->name('form');
        Route::post('/store', [PeminjamanController::class, 'store'])->name('store');
    });

    Route::prefix('baca-online')->name('baca.')->group(function () {
        Route::get('/', [BacaOnlineController::class, 'index'])->name('index');
        Route::get('/{id}/ringkasan', [BacaOnlineController::class, 'ringkasan'])->name('ringkasan');
        Route::get('/{id}/request', [BacaOnlineController::class, 'showCodeRequestPage'])->name('request_code');
        Route::post('/{id}/verify', [BacaOnlineController::class, 'verifyCode'])->name('verify_code');
        Route::get('/{id}/view', [BacaOnlineController::class, 'viewBook'])->name('view_book');
    });

    Route::get('/riwayat/offline', [RiwayatController::class, 'offlineIndex'])->name('riwayat.offline');
    Route::get('/riwayat/online', [RiwayatController::class, 'onlineIndex'])->name('riwayat.online');

    Route::get('/secure-pdf/{id}', [BacaOnlineController::class, 'streamPdf'])->name('baca.stream_pdf');

    // --- Manajemen Profil Pengguna ---
    Route::prefix('profile')->name('profile.')->group(function () {
        Route::get('/', [ProfileController::class, 'index'])->name('index');
        Route::get('/edit', [ProfileController::class, 'edit'])->name('edit');
        Route::patch('/', [ProfileController::class, 'update'])->name('update');
        Route::delete('/', [ProfileController::class, 'destroy'])->name('destroy');
    });
});

// --- GRUP RUTE KHUSUS UNTUK ADMIN / PENJAGA PERPUSTAKAAN ---
Route::middleware(['session.auth', 'role:penjaga perpus'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard');
    Route::get('/buku', [AdminBookController::class, 'index'])->name('buku.index');
    Route::get('/buku/{id}/edit', [AdminBookController::class, 'edit'])->name('buku.edit');
    Route::get('/buku/tambah', [AdminBookController::class, 'create'])->name('buku.create');
    Route::get('/pengguna', [AdminUserController::class, 'index'])->name('pengguna.index');
    Route::get('/pengumuman', [PengumumanController::class, 'index'])->name('pengumuman.index');
    Route::get('/pengumuman/tambah', [PengumumanController::class, 'create'])->name('pengumuman.create');
    Route::get('/pengumuman/{id}/edit', [PengumumanController::class, 'edit'])->name('pengumuman.edit');
    Route::get('/pengguna/tambah', [AdminUserController::class, 'create'])->name('pengguna.create');
    Route::get('/pengguna/{id}/edit', [AdminUserController::class, 'edit'])->name('pengguna.edit');
});

// GRUP RUTE KHUSUS UNTUK GURU
Route::middleware(['role:guru'])->prefix('guru')->name('guru.')->group(function () {
    Route::get('/laporan-minat-baca', [LaporanController::class, 'index'])->name('laporan.index');
});

require __DIR__ . '/auth.php';
