<?php

use App\Http\Controllers\AboutController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\BookController;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\IndexController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\NewPasswordController;
use App\Http\Controllers\SearchController;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Session;
/*
|--------------------------------------------------------------------------
| 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('/', [IndexController::class, 'create'])->name('accueil');
Route::get('about', [AboutController::class, 'create']);
Route::get('contact', [ContactController::class, 'create'])->name('contact'); //Relatif à IITECH en mode non connexion

//Login routes
Route::post('/user/login', [UserController::class, 'login'])->name('login');
Route::get('/user/login/{token}', [UserController::class, 'login_token']);

Route::get('/admin/login', function () {
    if (session()->has('BOOKCASE_ADMIN'))
        return redirect()->route('admin');
    return view('admin.login');
})->name('admin.login');

Route::post('/admin/login', [AdminController::class, 'login'])->name('admin_log');

Route::get('logout', function () {
    Session::forget('BOOKCASE_ADMIN');
    Session::forget('BOOKCASE_USER');
    if (Session::has('BOOKCASE_TOKEN_REDIRECT')) {
        return Redirect::to(Session::get('BOOKCASE_TOKEN_REDIRECT'));
    }
    return redirect()->route('accueil');
})->name('logout');

/**Inscription et connexion */
Route::group(['middleware' => ['load_tenant']], function () {

    /**Rechercher un livre */
    Route::post('/search', [SearchController::class, 'search'])->name('search');

    Route::prefix('user')->group(function () {

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

        Route::get('/books', [UserController::class, 'books'])->name('mes_livres');

        Route::get('/docs', [UserController::class, 'docs'])->name('ma_doc');
        Route::get('/docs/{matiere}', [UserController::class, 'docs_show'])->name('docs.show');

        Route::get('/save_add_book/{book}', [UserController::class, 'save_add_book'])->name('save_add_book');

        Route::get('/upload', [UserController::class, 'create_book'])->name('user_upload');
    });

    //Téléversement de livre
    Route::post('/upload', [BookController::class, 'store'])->name('add');

    //Suppresion de livre

    Route::delete('/admin/supp/{ISBN}', [BookController::class, 'delete'])->name('supp');

    Route::delete('/admin/suppDoc/{nom}', [BookController::class, 'deleteDoc'])->name('delete.doc');

    Route::delete('/admin/force/{ISBN}', [BookController::class, 'forceDestroy'])->name('book.force.supp');

    Route::delete('/admin/forceDoc/{nom}', [BookController::class, 'forceDestroyDoc'])->name('doc.force.supp');

    Route::put('/admin/restore/{ISBN}', [BookController::class, 'restore'])->name('book.restore');

    Route::put('/admin/restoreDoc/{nom}', [BookController::class, 'restoreDoc'])->name('doc.restore');

    Route::prefix('admin')->group(function () {

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

        Route::get('/book/edit/{id}', [BookController::class, 'edit'])->name('edit.book');

        Route::put('/book/update/{id}', [BookController::class, 'update'])->name('update.book');

        Route::get('/doc/edit/{nom}', [BookController::class, 'editDoc'])->name('edit.doc');

        Route::put('/doc/update/{id}', [BookController::class, 'updateDoc'])->name('update.doc');

        Route::get('/add', [AdminController::class, 'add'])->name('add_create');

        Route::get('/add_document', [BookController::class, 'createDoc'])->name('add.document');
        Route::post('/add_document', [BookController::class, 'storeDoc'])->name('store.document');

        Route::get('/attente', [AdminController::class, 'attente']);

        Route::get('/consulter_book', [AdminController::class, 'afficherLivres'])->name("all.books");
        Route::get('/consulter', [AdminController::class, 'consulterDoc'])->name('cons.doc');

        Route::get('/pages-faq', [AdminController::class, 'faq']);

        Route::get('/supp', [AdminController::class, 'supp'])->name('supp_create');

        Route::get('/supp_document', [AdminController::class, 'supp_document'])->name('supp.document');

        Route::get('/users-profile/{num}', [AdminController::class, 'profile'])->name('profil');

        Route::post('/users-profile', [AdminController::class, 'changePass']);

        Route::post('/upload-profile', [AdminController::class, 'changeProfile'])->name('changeprofile');

        Route::get('/messages', [AdminController::class, 'messages'])->name('inbox');
        Route::get('/messages/read/{id}', [AdminController::class, 'msg_read'])->name('read_msg');
        Route::delete('/messages/del/{id}', [AdminController::class, 'msg_delete'])->name('del_msg');
    });

    Route::post('/contact', function () {
        App\Models\Contact::create([
            'vnom' => request('name'),
            'email' => request('email'),
            'objet' => request('subject'),
            'commentaire' => request('comment'),
        ]);
        return Redirect()->route('contact')->with('success', 'Message envoyé');
    });

    //Lecture d'un livre
    Route::get('discipline/{disname}/books', [BookController::class, 'index'])->name('discipline_book');

    Route::get('books/read/{id}', [BookController::class, 'show'])->name('read.book');

    Route::get('docs/read/{id}', [BookController::class, 'show_doc'])->name('read.doc');

    Route::get('books/retrait_book_emprunt/{book}', [BookController::class, 'retrait_book'])->name('retrait_book');

    Route::get('books/valid_ajout/{id}', [BookController::class, 'valid_store_user'])->name('book.valid.ajout');
});
