<?php

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

    Route::get('/dashboard', [\App\Http\Controllers\DashboardController::class, 'index'])->name('dashboard');

    // User Access for Loans (Self Service)
    Route::get('/user/loans', [\App\Http\Controllers\UserLoanController::class, 'index'])->name('user.loans.index');
    Route::post('/user/loans', [\App\Http\Controllers\UserLoanController::class, 'store'])->name('user.loans.store');
    Route::post('/user/loans/return-by-code', [\App\Http\Controllers\UserLoanController::class, 'returnByCode'])->name('user.loans.return-by-code');
    Route::post('/user/loans/{id}/return', [\App\Http\Controllers\UserLoanController::class, 'returnItem'])->name('user.loans.return');

    // Admin & Operator Access
    // Admin & Operator Access (Shared)
    Route::middleware('role:admin,operator')->group(function () {
        Route::get('/loans/active-by-code/{code}', [\App\Http\Controllers\LoanController::class, 'getActiveLoansBySparepartCode']);
        Route::resource('loans', \App\Http\Controllers\LoanController::class);
        Route::resource('sparepart-transactions', \App\Http\Controllers\SparepartTransactionController::class);
    });

    // Admin Only Access
    Route::middleware('role:admin')->group(function () {
        Route::resource('users', \App\Http\Controllers\UserController::class);
        Route::resource('employees', \App\Http\Controllers\EmployeeController::class);
        Route::resource('spareparts', \App\Http\Controllers\SparepartController::class);
        Route::get('/reports', [\App\Http\Controllers\ReportController::class, 'index'])->name('reports.index');
        Route::get('/reports/sparepart-transactions', [\App\Http\Controllers\ReportController::class, 'sparepartTransactions'])->name('reports.sparepart_transactions');
    });
});

require __DIR__.'/auth.php';
