<?php

use App\Http\Controllers\BookController;
use App\Http\Controllers\BorrowController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\LanguageController;
use App\Http\Controllers\LocationController;
use App\Http\Controllers\MemberController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\SubCategoryController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\WasteController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

Route::get('/ps', function () {
    return redirect()->route('login');
});
Route::redirect('/home', '/ps/home');
// Auth::routes(['register' => false]);
Auth::routes();
Route::get('change-language/{lang}', function ($lang) {
    $previousRoute = url()->previous();
    $previousRouteSegments = explode('/', $previousRoute);
    $currentLang = $previousRouteSegments[3];
    $redirectUrl = str_replace("/$currentLang", "/$lang", $previousRoute);
    return redirect($redirectUrl);
})->name('change-language');

Route::prefix('{locale}')->group(function () {
    Route::middleware(['auth', 'has-permission'])->group(function () {
        Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

        Route::group(['prefix' => '/settings'], function () {
            Route::resource('categories', CategoryController::class)->except(['create']);
            Route::get('categories-table', [CategoryController::class, 'dataTable'])->name('categories.dataTable');

            Route::resource('languages', LanguageController::class)->except(['create']);
            Route::get('languages-table', [LanguageController::class, 'dataTable'])->name('languages.dataTable');

            Route::resource('locations', LocationController::class)->except(['create']);
            Route::get('locations-table', [LocationController::class, 'dataTable'])->name('locations.dataTable');
        });

        Route::resource('books', BookController::class);
        Route::get('books-table', [BookController::class, 'dataTable'])->name('books.dataTable');

        Route::resource('members', MemberController::class);
        Route::get('members-table', [MemberController::class, 'dataTable'])->name('members.dataTable');

        Route::resource('borrows', BorrowController::class);
        Route::get('borrows-table', [BorrowController::class, 'dataTable'])->name('borrows.dataTable');
        Route::get('return-borrow/{id}', [BorrowController::class, 'return'])->name('borrows.return');

        Route::resource('wastes', WasteController::class);
        Route::get('wastes-table', [WasteController::class, 'dataTable'])->name('wastes.dataTable');


        Route::resource('users', UserController::class);
        Route::get('/logs/data', [UserController::class, 'getLogs'])->name('users.logs');


        Route::view('change-password', 'user.changePassword')->name('users.setting');
        Route::post('user/change-password', [UserController::class, 'changePassword'])->name('users.change-password');
        Route::get('update-user-status', [UserController::class, 'updateUserStatus'])->name('users.update-user-status');
        Route::get('/backup-database', [HomeController::class, 'takeBackup'])->name('backup.database');
        Route::get('/backup/download', [HomeController::class, 'downloadStorageBackup'])->name('backup.download');
        Route::post('role', [RoleController::class, 'store'])->name('role.store');
        Route::get('role/{id}/edit', [RoleController::class, 'edit'])->name('role.edit');
        Route::put('role/{id}', [RoleController::class, 'update'])->name('role.update');
        Route::get('get-permissions', [RoleController::class, 'getPermissions'])->name('role.get-permissions');
        Route::post('give-permissions', [RoleController::class, 'givePermissions'])->name('role.give-permissions');
        Route::post('give-role', [RoleController::class, 'giveRole'])->name('users.give-role');
    });
});
//just for testing purpose
Route::get('run-migration', function () {
    \Artisan::call('migrate');
    \Artisan::call('db:seed');
    return redirect()->back()->with('success', 'Migration completed successfully.');
})->name('run-migration');
Route::get('clear', function () {
    \Artisan::call('config:clear');
    \Artisan::call('cache:clear');
    echo "Cache Cleared!";
})->name('clear');
