<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BookController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\LoanController;
use App\Http\Controllers\PenaltyController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\ProfileController;

/*
  |--------------------------------------------------------------------------
  | Web Routes
  |--------------------------------------------------------------------------
  |
  | Here is where you can register web routes for your application. These
  | routes are loaded by the RouteServiceProvider within a group which
  | contains the "web" middleware group. Now create something great!
  |
 */

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

Auth::routes();

Route::resources([
    'books' => BookController::class,
    'categories' => CategoryController::class
        ], [
    'only' => ['index', 'show']
]);

Route::post('/books/search', [BookController::class, 'search'])
        ->name('books.search');
Route::post('/categories/search', [CategoryController::class, 'search'])
        ->name('categories.search');
Route::post('/categories/{category}/search', [CategoryController::class, 'searchBooks'])
        ->name('categories.searchBooks');

Route::group([
    'middleware' => 'auth',
    'prefix' => 'auth'
        ], function () {
    Route::resources([
        'loans' => LoanController::class,
        'penalties' => PenaltyController::class
            ], [
        'only' => ['index', 'show']
    ]);

    Route::post('/loans/search', [LoanController::class, 'search'])
            ->name('loans.search');
    Route::post('/penalties/search', [PenaltyController::class, 'search'])
            ->name('penalties.search');

    Route::controller(ProfileController::class)->group(function () {
        Route::get('/profiles/{user}', 'show')->name('profiles.show');
        Route::get('/profiles/{user}/edit', 'edit')->name('profiles.edit');
        Route::match(['put', 'patch'], '/profiles/{user}', 'update')
                ->name('profiles.update');
    });
});

Route::group([
    'middleware' => 'role:Administrator',
    'prefix' => 'admin'
        ], function () {
    Route::resources([
        'books' => BookController::class,
        'categories' => CategoryController::class,
        'loans' => LoanController::class,
        'penalties' => PenaltyController::class
            ], [
        'except' => ['index', 'show']
    ]);

    Route::resource('users', UserController::class);
    Route::post('/users/search', [UserController::class, 'search'])
            ->name('users.search');
});
