<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\AdministrationController;
use App\Http\Controllers\AuditController;
use App\Http\Controllers\BranchContextController;
use App\Http\Controllers\BranchController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\DepartmentController;
use App\Http\Controllers\FoundationController;
use App\Http\Controllers\GlobalSearchController;
use App\Http\Controllers\OperatingModeController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\SettingController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::redirect('/', '/painel');

Route::middleware('guest')->group(function (): void {
    Route::get('/entrar', [AuthController::class, 'create'])->name('login');
    Route::post('/entrar', [AuthController::class, 'store'])->name('login.store');
});

Route::middleware(['auth', 'password.changed', 'mode.valid'])->group(function (): void {
    Route::get('/painel', DashboardController::class)->name('dashboard');
    Route::get('/fundacao', FoundationController::class)->middleware('permission:foundation.view')->name('foundation');
    Route::get('/pesquisa', GlobalSearchController::class)->middleware('permission:search.use')->name('search');
    Route::get('/auditoria', [AuditController::class, 'index'])->middleware('permission:audit.view')->name('admin.audit.index');
    Route::post('/modo-de-trabalho', [OperatingModeController::class, 'update'])->name('operating-mode.update');
    Route::post('/filial/{branch}', [BranchContextController::class, 'update'])->name('branch-context.update');

    Route::prefix('administracao')->name('admin.')->middleware('admin.mode')->group(function (): void {
        Route::get('/', AdministrationController::class)->name('index');

        Route::resource('filiais', BranchController::class)
            ->parameters(['filiais' => 'branch'])
            ->names('branches')
            ->except('show')
            ->middleware('permission:branches.manage');

        Route::resource('departamentos', DepartmentController::class)
            ->parameters(['departamentos' => 'department'])
            ->names('departments')
            ->except('show')
            ->middleware('permission:departments.manage');

        Route::resource('utilizadores', UserController::class)
            ->parameters(['utilizadores' => 'user'])
            ->names('users')
            ->except('show')
            ->middleware('permission:users.manage');
        Route::post('/utilizadores/{user}/desbloquear', [UserController::class, 'unlock'])
            ->middleware('permission:users.manage')->name('users.unlock');

        Route::resource('perfis', RoleController::class)
            ->parameters(['perfis' => 'role'])
            ->names('roles')
            ->except('show')
            ->middleware('permission:roles.manage');

        Route::get('/configuracoes', [SettingController::class, 'index'])
            ->middleware('permission:settings.manage')->name('settings.index');
        Route::put('/configuracoes/{setting}', [SettingController::class, 'update'])
            ->middleware('permission:settings.manage')->name('settings.update');
    });
});

Route::middleware('auth')->group(function (): void {
    Route::get('/palavra-passe', [AuthController::class, 'editPassword'])->name('password.edit');
    Route::put('/palavra-passe', [AuthController::class, 'updatePassword'])->name('password.update');
    Route::post('/sair', [AuthController::class, 'destroy'])->name('logout');
});
