<?php

use App\Modules\Settings\Rbac\Controllers\TenantRolesPageController;
use App\Modules\Settings\Rbac\Controllers\TenantRoleWriteController;
use App\Modules\Settings\Rbac\Controllers\TenantUserEditController;
use App\Modules\Settings\Rbac\Controllers\TenantUsersPageController;
use Illuminate\Support\Facades\Route;

Route::prefix('settings')->name('settings.')->group(function () {
    Route::prefix('users')->name('users.')->group(function () {
        Route::get('/', [TenantUsersPageController::class, 'index'])
            ->middleware('can:settings.users.view')
            ->name('index');

        Route::get('/create', [TenantUsersPageController::class, 'create'])
            ->middleware('can:settings.users.invite')
            ->name('create');

        Route::post('/', [TenantUsersPageController::class, 'store'])
            ->middleware('can:settings.users.invite')
            ->name('store');

        Route::get('/invite', [TenantUsersPageController::class, 'invite'])
            ->middleware('can:settings.users.invite')
            ->name('invite');

        Route::post('/invite', [TenantUsersPageController::class, 'storeInvite'])
            ->middleware('can:settings.users.invite')
            ->name('invite.store');

        Route::get('/{user}/edit', [TenantUserEditController::class, 'edit'])
            ->whereNumber('user')
            ->middleware('can:settings.users.assign_roles')
            ->name('edit');

        Route::put('/{user}', [TenantUserEditController::class, 'update'])
            ->whereNumber('user')
            ->middleware('can:settings.users.assign_roles')
            ->name('update');

        Route::put('/{user}/password', [TenantUserEditController::class, 'resetPassword'])
            ->whereNumber('user')
            ->middleware('can:settings.users.assign_roles')
            ->name('password.update');

        Route::post('/{user}/deactivate', [TenantUserEditController::class, 'deactivate'])
            ->whereNumber('user')
            ->middleware('can:settings.users.assign_roles')
            ->name('deactivate');

        Route::post('/{user}/activate', [TenantUserEditController::class, 'activate'])
            ->whereNumber('user')
            ->middleware('can:settings.users.assign_roles')
            ->name('activate');
    });

    Route::prefix('roles')->name('roles.')->group(function () {
        Route::get('/', [TenantRolesPageController::class, 'index'])
            ->middleware('can:settings.roles.view')
            ->name('index');

        Route::get('/create', [TenantRolesPageController::class, 'create'])
            ->middleware('can:settings.roles.manage')
            ->name('create');

        Route::post('/', [TenantRoleWriteController::class, 'store'])
            ->middleware('can:settings.roles.manage')
            ->name('store');

        Route::get('/{role}', [TenantRolesPageController::class, 'show'])
            ->whereNumber('role')
            ->middleware('can:settings.roles.view')
            ->name('show');

        Route::get('/{role}/edit', [TenantRolesPageController::class, 'edit'])
            ->whereNumber('role')
            ->middleware('can:settings.roles.manage')
            ->name('edit');

        Route::put('/{role}', [TenantRoleWriteController::class, 'update'])
            ->whereNumber('role')
            ->middleware('can:settings.roles.manage')
            ->name('update');

        Route::delete('/{role}', [TenantRoleWriteController::class, 'destroy'])
            ->whereNumber('role')
            ->middleware('can:settings.roles.manage')
            ->name('destroy');
    });
});
