<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Livewire\Home;
use App\Livewire\Territori\TerritorioIndex;
use App\Livewire\Territori\TerritorioCreate;
use App\Livewire\Territori\TerritorioShow;
use App\Livewire\Territori\TerritorioEdit;
use App\Livewire\Territori\TerritorioCestino;
use App\Livewire\Proclamatori\ProclamatoreIndex;
use App\Livewire\Proclamatori\ProclamatoreCreate;
use App\Livewire\Proclamatori\ProclamatoreShow;
use App\Livewire\Proclamatori\ProclamatoreEdit;
use App\Livewire\Proclamatori\ProclamatoreCestino;
use App\Livewire\Assegnazioni\Assegna;
use App\Livewire\Assegnazioni\Rientra;
use App\Livewire\Campagne\CampagnaIndex;
use App\Livewire\Campagne\CampagnaCreate;
use App\Livewire\Campagne\CampagnaShow;
use App\Livewire\Campagne\CampagnaEdit;
use App\Livewire\Registro;
use App\Livewire\AuditLog;
use App\Livewire\Settings\SettingsEdit;
use App\Livewire\Settings\ZoneIndex;
use App\Livewire\Settings\TipologieIndex;
use App\Livewire\Settings\UsersIndex;
use App\Livewire\Settings\XmlExchange;
use App\Livewire\Privacy;

/*
|--------------------------------------------------------------------------
| Authentication Routes
|--------------------------------------------------------------------------
*/
Route::middleware('guest')->group(function () {
    Route::get('login', App\Livewire\Auth\Login::class)->name('login');
    Route::post('login', LoginController::class)->name('login.store');
});

Route::post('logout', function () {
    activity()->causedBy(auth()->user())->log('logout');
    auth()->logout();
    request()->session()->invalidate();
    request()->session()->regenerateToken();
    return redirect('/login');
})->middleware('auth')->name('logout');

/*
|--------------------------------------------------------------------------
| Authenticated Routes
|--------------------------------------------------------------------------
*/
Route::middleware('auth')->group(function () {

    // Home Dashboard
    Route::get('/', Home::class)->name('dashboard');

    // Territori
    Route::middleware('permission:territori.manage')->group(function () {
        Route::get('territori', TerritorioIndex::class)->name('territori.index');
        Route::get('territori/create', TerritorioCreate::class)->name('territori.create');
        Route::get('territori/cestino', TerritorioCestino::class)->name('territori.cestino');
        Route::get('territori/{territorio}', TerritorioShow::class)->name('territori.show');
        Route::get('territori/{territorio}/edit', TerritorioEdit::class)->name('territori.edit');
    });

    // Proclamatori
    Route::middleware('permission:proclamatori.manage')->group(function () {
        Route::get('proclamatori', ProclamatoreIndex::class)->name('proclamatori.index');
        Route::get('proclamatori/create', ProclamatoreCreate::class)->name('proclamatori.create');
        Route::get('proclamatori/cestino', ProclamatoreCestino::class)->name('proclamatori.cestino');
        Route::get('proclamatori/{proclamatore}', ProclamatoreShow::class)->name('proclamatori.show');
        Route::get('proclamatori/{proclamatore}/edit', ProclamatoreEdit::class)->name('proclamatori.edit');
    });

    // Assegnazioni
    Route::middleware('permission:territori.assign')->group(function () {
        Route::get('assegnazioni/assegna/{territorioId?}', Assegna::class)->name('assegnazioni.assegna');
    });
    Route::middleware('permission:territori.return')->group(function () {
        Route::get('assegnazioni/{assegnazione}/rientra', Rientra::class)->name('assegnazioni.rientra');
    });

    // Campagne
    Route::middleware('permission:campagne.manage')->group(function () {
        Route::get('campagne', CampagnaIndex::class)->name('campagne.index');
        Route::get('campagne/create', CampagnaCreate::class)->name('campagne.create');
        Route::get('campagne/{campagna}', CampagnaShow::class)->name('campagne.show');
        Route::get('campagne/{campagna}/edit', CampagnaEdit::class)->name('campagne.edit');
    });

    // Registro
    Route::middleware('permission:registro.view')->group(function () {
        Route::get('registro', Registro::class)->name('registro.index');
    });

    // Audit Log
    Route::middleware('permission:audit.view')->group(function () {
        Route::get('audit', AuditLog::class)->name('audit.index');
    });

    // Settings (admin)
    Route::middleware('permission:settings.manage')->group(function () {
        Route::get('impostazioni', SettingsEdit::class)->name('settings.edit');
        Route::get('utenti', UsersIndex::class)->name('users.index');
        Route::get('zone', ZoneIndex::class)->name('zone.index');
        Route::get('tipologie', TipologieIndex::class)->name('tipologie.index');
        Route::get('xml-exchange', XmlExchange::class)->name('xml.exchange');
    });

    // Privacy / Informativa GDPR
    Route::get('privacy', Privacy::class)->name('privacy');
});
