<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\PortalController;
use App\Http\Controllers\ModuloPlaceholderController;
use App\Http\Controllers\SistemaController;
use App\Http\Controllers\WikiEditorUploadController;
use App\Http\Controllers\WikiMediaController;
use App\Http\Livewire\Admin\Configuracoes;
use App\Http\Livewire\Admin\Departamentos;
use App\Http\Livewire\Admin\Usuarios;
use App\Http\Livewire\Wiki\WikiIndex;
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 and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return redirect()->route('dashboard');
});

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/dashboard', PortalController::class)
        ->middleware('can:portal.acessar')
        ->name('dashboard');

    Route::get('/wiki', WikiIndex::class)
        ->middleware('can:wiki.visualizar')
        ->name('wiki.index');

    Route::get('/wiki/departamentos', Departamentos::class)
        ->middleware(['role:Administrador Master'])
        ->name('wiki.departamentos');

    Route::post('/wiki/editor-upload', WikiEditorUploadController::class)
        ->middleware('can:wiki.visualizar')
        ->name('wiki.editor-upload');

    Route::get('/wiki/anexos/{anexo}/visualizar', [WikiMediaController::class, 'anexo'])
        ->middleware('can:wiki.visualizar')
        ->name('wiki.anexos.visualizar');

    Route::get('/wiki/editor-media/{path}', [WikiMediaController::class, 'editor'])
        ->middleware('can:wiki.visualizar')
        ->where('path', '.*')
        ->name('wiki.editor-media');

    Route::get('/wiki/departamento/{departamento}/categoria/{subcategoria}', function (int $departamento, int $subcategoria) {
        $departamento = \App\Models\Departamento::findOrFail($departamento);
        $subcategoria = \App\Models\Subcategoria::where('departamento_id', $departamento->id)->findOrFail($subcategoria);

        return redirect()->route('wiki.subcategoria', [$departamento->slug, $subcategoria->slug]);
    })->middleware('can:wiki.visualizar')->whereNumber(['departamento', 'subcategoria']);

    Route::get('/wiki/departamento/{departamento}', function (int $departamento) {
        $departamento = \App\Models\Departamento::findOrFail($departamento);

        return redirect()->route('wiki.departamento', $departamento->slug);
    })->middleware('can:wiki.visualizar')->whereNumber('departamento');

    Route::get('/wiki/{departamento}', WikiIndex::class)
        ->middleware('can:wiki.visualizar')
        ->name('wiki.departamento');

    Route::get('/wiki/{departamento}/{subcategoria}', WikiIndex::class)
        ->middleware('can:wiki.visualizar')
        ->name('wiki.subcategoria');

    Route::redirect('/admin/departamentos', '/wiki/departamentos');

    Route::get('/admin/usuarios', Usuarios::class)
        ->middleware(['role:Administrador Master'])
        ->name('admin.usuarios');

    Route::get('/admin/configuracoes', Configuracoes::class)
        ->middleware('can:sistema.configurar')
        ->name('admin.configuracoes');

    Route::get('/modulos/{slug}', ModuloPlaceholderController::class)
        ->middleware('can:modulos.visualizar')
        ->name('modulos.placeholder');

    Route::redirect('/sistema/configuracoes', '/admin/configuracoes');

    Route::get('/sistema/{slug}', [SistemaController::class, 'show'])
        ->name('sistema.show');

    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');
});

require __DIR__.'/auth.php';
