<?php

use App\Http\Controllers\BarcodeController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\SubcategoryController;
use App\Http\Controllers\UsersController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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('/', function () {
    return redirect('/login');
});

// Login
Route::get('/login', [UsersController::class, 'showLogin'])->name('login')->middleware('guest');
Route::post('/login-check', [UsersController::class, 'login'])->name('login.check');

Route::get('/unauthorized', [Controller::class, 'unauthorized'])->name('unauthorized');

// Protected routes
Route::middleware('auth')->group(function () {

    Route::get('/dashboard', [UsersController::class, 'dashboard'])->name('dashboard')->middleware('role:Admin,MD');

    Route::post('/logout', [UsersController::class, 'logout'])->name('logout');

    Route::middleware('role:Admin')->prefix('category/')->group(function () {
        Route::get('list', [CategoryController::class, 'index'])->name('category_list');
        Route::get('create', [CategoryController::class, 'create'])->name('category_create');
        Route::post('store', [CategoryController::class, 'store'])->name('category_store');
        Route::get('edit/{id}', [CategoryController::class, 'edit'])->name('category_edit');
        Route::post('update', [CategoryController::class, 'update'])->name('category_update');
        Route::get('delete/{id}', [CategoryController::class, 'delete'])->name('category_delete');
    });
    Route::get('/category/options', [CategoryController::class, 'options'])->name('category_options')->middleware('role:Admin,Staff,HOD,MD');

    Route::middleware('role:Admin')->prefix('subcategory/')->group(function () {
        Route::get('list', [SubcategoryController::class, 'index'])->name('subcategory_list');
        Route::get('create', [SubcategoryController::class, 'create'])->name('subcategory_create');
        Route::post('store', [SubcategoryController::class, 'store'])->name('subcategory_store');
        Route::get('edit/{id}', [SubcategoryController::class, 'edit'])->name('subcategory_edit');
        Route::post('update', [SubcategoryController::class, 'update'])->name('subcategory_update');
        Route::get('delete/{id}', [SubcategoryController::class, 'delete'])->name('subcategory_delete');
    });
    Route::get('/subcategory/options', [SubcategoryController::class, 'options'])->name('subcategory_options')->middleware('role:Admin,Staff,HOD,MD');

    Route::prefix('barcode/')->group(function () {
        Route::get('list', [BarcodeController::class, 'index'])->name('barcode_list')->middleware('role:Admin,Staff,HOD,MD');
        Route::get('create', [BarcodeController::class, 'create'])->name('barcode_create')->middleware('role:Admin,Staff');
        Route::post('store', [BarcodeController::class, 'store'])->name('barcode_store')->middleware('role:Admin,Staff');
        Route::get('show/{id}', [BarcodeController::class, 'show'])->name('barcode_show')->middleware('role:Admin,Staff,HOD,MD');
        Route::get('edit/{id}', [BarcodeController::class, 'edit'])->name('barcode_edit')->middleware('role:Admin,Staff');
        Route::post('update', [BarcodeController::class, 'update'])->name('barcode_update')->middleware('role:Admin,Staff');
        Route::get('delete/{id}', [BarcodeController::class, 'delete'])->name('barcode_delete')->middleware('role:Admin,Staff');

        Route::get('send-mail', [BarcodeController::class, 'sendMail'])->name('send_mail')->middleware('role:Admin');
    });
});
