<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\WelcomeController;
use App\Http\Controllers\BukuTamuController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\User\DashboardController as UserDashboardController;
use App\Http\Controllers\AnggotaController;
use App\Http\Controllers\BukuController;
use App\Http\Controllers\PeminjamanController;
use App\Http\Controllers\LaporanController;
use App\Http\Controllers\VisitorKatalogController;
use App\Http\Controllers\AdminBukuController;
use App\Http\Controllers\AdminPeminjamanController;
use App\Http\Controllers\AdminAkunController;

/*
|--------------------------------------------------------------------------
| PUBLIC ROUTES (Halaman Depan & Login)
|--------------------------------------------------------------------------
*/

// Landing page
Route::get('/', [WelcomeController::class, 'index'])->name('home');

// Buku Tamu (Form Pengunjung)
Route::get('/buku-tamu', [BukuTamuController::class, 'index'])->name('buku_tamu.index');
Route::post('/buku-tamu', [BukuTamuController::class, 'store'])->name('buku_tamu.store');


// Auth Routes
Route::get('/login', [LoginController::class, 'index'])->name('login');
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');
Route::get('/register', [RegisterController::class, 'showRegistrationForm'])->name('register');
Route::post('/register', [RegisterController::class, 'register'])->name('register.process');

// Login Specifics
Route::get('/login/admin', [LoginController::class, 'showAdminLoginForm'])->name('login.admin');
Route::post('/login/admin', [LoginController::class, 'adminLogin'])->name('login.admin.process');
Route::get('/login/user', [LoginController::class, 'showUserLoginForm'])->name('login.user');
Route::post('/login/user', [LoginController::class, 'userLogin'])->name('login.user.process');

Route::get('/katalog', [VisitorKatalogController::class, 'index'])->name('katalog.index');
Route::get('/katalog/{id}', [VisitorKatalogController::class, 'show'])->name('katalog.show');


/*
|--------------------------------------------------------------------------
| USER ROUTES (Member Login)
|--------------------------------------------------------------------------
*/
Route::middleware(['auth'])->group(function () {
    Route::get('/user/dashboard', [UserDashboardController::class, 'index'])->name('user.dashboard');
});


/*
|--------------------------------------------------------------------------
| ADMIN ROUTES (Sidebar Menu)
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'admin']) // Pastikan middleware 'admin' sudah terdaftar di Kernel
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {

        // 1. DASHBOARD
        Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard');

        // MANAJEMEN AKUN ADMIN
        Route::get('/akun', [AdminAkunController::class, 'index'])->name('akun.index');
        Route::post('/akun', [AdminAkunController::class, 'store'])->name('akun.store');
        Route::delete('/akun/{id}', [AdminAkunController::class, 'destroy'])->name('akun.destroy');

        // 2. MENU DATA BUKU
        // Route ini otomatis membuat: admin.buku.index, admin.buku.create, dll.
        // Route::resource('buku', BukuController::class);

        // 3. MENU DATA ANGGOTA (Dropdown)
        Route::prefix('anggota')->name('anggota.')->group(function() {
            // Sub-menu: Data Member (Resource)
            Route::resource('member', AnggotaController::class); 
            
            // Sub-menu: Buku Tamu (Admin View - Melihat daftar hadir)
            // Pastikan bikin method 'adminList' di BukuTamuController ya!
            Route::get('/tamu', [BukuTamuController::class, 'adminList'])->name('tamu'); 
        });

        // 4. MENU PEMINJAMAN
        Route::get('/peminjaman', [AdminPeminjamanController::class, 'index'])->name('peminjaman.index');
        Route::get('/peminjaman/scan', [AdminPeminjamanController::class, 'scan'])->name('peminjaman.scan');
        Route::post('/peminjaman/scan', [AdminPeminjamanController::class, 'prosesScan'])->name('peminjaman.proses_scan');
        Route::get('/peminjaman/tambah', [AdminPeminjamanController::class, 'create'])->name('peminjaman.create');
        Route::post('/peminjaman', [AdminPeminjamanController::class, 'store'])->name('peminjaman.store');
        Route::get('/peminjaman/{id}/edit', [AdminPeminjamanController::class, 'edit'])->name('peminjaman.edit');
        Route::put('/peminjaman/{id}', [AdminPeminjamanController::class, 'update'])->name('peminjaman.update');
        Route::delete('/peminjaman/{id}', [AdminPeminjamanController::class, 'destroy'])->name('peminjaman.destroy');
        Route::put('/peminjaman/{id}/kembali', [AdminPeminjamanController::class, 'kembalikan'])->name('peminjaman.kembalikan');
        
        // Shortcut routes (optional compatibility)
        Route::get('/transaksi-peminjaman', [AdminPeminjamanController::class, 'index']);
        Route::get('/transaksi-peminjaman/tambah', [AdminPeminjamanController::class, 'create']);
        Route::put('/transaksi-peminjaman/{id}', [AdminPeminjamanController::class, 'update']);
        Route::delete('/transaksi-peminjaman/{id}', [AdminPeminjamanController::class, 'destroy']);
        Route::get('/peminjaman/{id}', function($id) { return redirect()->route('admin.peminjaman.index'); });
        Route::get('/transaksi-peminjaman/{id}', function($id) { return redirect()->route('admin.peminjaman.index'); });

        // 5. MENU PENGEMBALIAN
        // Route khusus untuk memproses pengembalian buku
        Route::put('peminjaman/{peminjaman}/kembali', [PeminjamanController::class, 'kembali'])->name('peminjaman.kembali');
        // Route untuk halaman list pengembalian (jika dipisah dari peminjaman)
        Route::get('/pengembalian', [PeminjamanController::class, 'indexPengembalian'])->name('pengembalian.index');

        // 6. MENU LAPORAN (Dropdown)
        Route::prefix('laporan')->name('laporan.')->group(function() {
            // Sub-menu: Laporan Kehadiran
            Route::get('/kehadiran', [LaporanController::class, 'kehadiran'])->name('kehadiran');
            // Sub-menu: Laporan Peminjaman
            Route::get('/peminjaman', [LaporanController::class, 'peminjaman'])->name('peminjaman');
        });

});

Route::get('/admin/buku/tambah', [AdminBukuController::class, 'create'])->name('admin.buku.create');
Route::post('/admin/buku', [AdminBukuController::class, 'store'])->name('admin.buku.store');
Route::get('/admin/buku', [AdminBukuController::class, 'index'])->name('admin.buku.index');
Route::get('/admin/buku/{id}/edit', [AdminBukuController::class, 'edit'])->name('admin.buku.edit');
Route::put('/admin/buku/{id}', [AdminBukuController::class, 'update'])->name('admin.buku.update');
Route::delete('/admin/buku/{id}', [AdminBukuController::class, 'destroy'])->name('admin.buku.destroy');
Route::get('/admin/peminjaman/{id}/struk', [AdminPeminjamanController::class, 'cetakStruk'])->name('admin.peminjaman.struk');
Route::post('/admin/peminjaman/{id}/resend-wa', [AdminPeminjamanController::class, 'resendWa'])->name('admin.peminjaman.resend_wa');