<?php

use App\Http\Controllers\Admin\AdminPeminjamanController;
use App\Http\Controllers\Admin\AdminRekomendasiController;
use Illuminate\Support\Facades\Route;

// General Controllers
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;

// Admin Controllers
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\BookController as AdminBookController;
use App\Http\Controllers\Admin\MasterIndukController;
use App\Http\Controllers\Admin\UserController as AdminUserController;
use App\Http\Controllers\Admin\PengumumanController as AdminPengumumanController;
use App\Http\Controllers\Admin\MasterIndukController as AdminMasterIndukController;
use App\Http\Controllers\RekomendasiController;
// Guru Controller
use App\Http\Controllers\Guru\LaporanController;

// Auth Controllers
use App\Http\Controllers\Auth\AdminLoginController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

// RUTE PUBLIK (Bisa diakses tanpa login)
Route::get('/', function () {
    return view('welcome');
});

// --- RUTE UNTUK PENGGUNA TERAUTENTIKASI (SISWA, GURU, & PENJAGA PERPUS) ---
Route::middleware(['auth'])->group(function () {

    // Rute Umum untuk Siswa & Guru
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/katalog', [KatalogController::class, 'index'])->name('katalog.index');
    Route::get('/rekomendasi/{id}', [RekomendasiController::class, 'show'])->name('rekomendasi.show');

    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('/secure-pdf/{id}', [BacaOnlineController::class, 'streamPdf'])->name('stream_pdf');
    });

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

    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 GURU ---
    Route::middleware(['role:guru'])->prefix('guru')->name('guru.')->group(function () {
        Route::get('/laporan-minat-baca', [LaporanController::class, 'index'])->name('laporan.index');
    });
});

// --- GRUP RUTE KHUSUS UNTUK ADMIN / PENJAGA PERPUSTAKAAN ---
Route::middleware(['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/tambah', [AdminBookController::class, 'create'])->name('buku.create');
    Route::post('/buku', [AdminBookController::class, 'store'])->name('buku.store');
    Route::get('/buku/{id}/edit', [AdminBookController::class, 'edit'])->name('buku.edit');

    Route::get('/pengguna', [AdminUserController::class, 'index'])->name('pengguna.index');
    Route::get('/pengguna/tambah', [AdminUserController::class, 'create'])->name('pengguna.create');
    Route::post('/pengguna', [AdminUserController::class, 'store'])->name('pengguna.store');
    Route::get('/pengguna/{id}/edit', [AdminUserController::class, 'edit'])->name('pengguna.edit');
    Route::patch('/pengguna/{id}', [AdminUserController::class, 'update'])->name('pengguna.update');
    Route::delete('/pengguna/{id}', [AdminUserController::class, 'destroy'])->name('pengguna.destroy');

    Route::get('/pengumuman', [AdminPengumumanController::class, 'index'])->name('pengumuman.index');
    Route::get('/pengumuman/tambah', [AdminPengumumanController::class, 'create'])->name('pengumuman.create');
    Route::post('/pengumuman', [AdminPengumumanController::class, 'store'])->name('pengumuman.store');
    Route::get('/pengumuman/{id}/edit', [AdminPengumumanController::class, 'edit'])->name('pengumuman.edit');
    Route::put('/pengumuman/{id}', [AdminPengumumanController::class, 'update'])->name('pengumuman.update');
    Route::delete('/pengumuman/{id}', [AdminPengumumanController::class, 'destroy'])->name('pengumuman.destroy');

    Route::get('/rekomendasi', [
        AdminRekomendasiController
        ::class,
        'index'
    ])->name('rekomendasi.index');
    Route::get('/rekomendasi/tambah', [AdminRekomendasiController::class, 'create'])->name('rekomendasi.create');
    Route::post('/rekomendasi', [AdminRekomendasiController::class, 'store'])->name('rekomendasi.store');
    Route::get('/rekomendasi/{id}/edit', [AdminRekomendasiController::class, 'edit'])->name('rekomendasi.edit');
    Route::put('/rekomendasi/{id}', [AdminRekomendasiController::class, 'update'])->name('rekomendasi.update');
    Route::delete('/rekomendasi/{id}', [AdminRekomendasiController::class, 'destroy'])->name('rekomendasi.destroy');

    Route::get('/peminjaman', [AdminPeminjamanController::class, 'index'])->name('peminjaman.index');
    Route::get('/peminjaman/tambah', [AdminPeminjamanController::class, 'create'])->name('peminjaman.create');

    Route::get('/denda', [AdminPeminjamanController::class, 'dendaIndex'])->name('denda.index');
    Route::post('/denda/sanksi', [AdminPeminjamanController::class, 'berikanSanksi'])->name('denda.sanksi');

    Route::post('/master-induk', [MasterIndukController::class, 'store'])->name('master-induk.store');
    Route::delete('/master-induk/{id}', [MasterIndukController::class, 'destroy'])->name('master-induk.destroy');
});

// --- RUTE LOGIN KHUSUS ADMIN ---
Route::middleware('guest')->group(function () {
    Route::get('/admin/login', [AdminLoginController::class, 'create'])->name('admin.login');
    Route::post('/admin/login', [AdminLoginController::class, 'store'])->name('admin.login.store');
});

// -- RUTE RESET PASSWORD --
Route::get('/reset-password', function () {
    return view('auth.reset-password-request');
})->name('reset.password-request');

require __DIR__ . '/auth.php';
