<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\AuthController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\IndividuoController;
use App\Http\Controllers\GruppoController;
use App\Http\Controllers\ContattoController;
use App\Http\Controllers\DocumentoController;
use App\Http\Controllers\GruppoIndividuoController;
use App\Http\Controllers\GruppoMembroController;
use App\Http\Controllers\EventoController;
use App\Http\Controllers\EventoDocumentoController;
use App\Http\Controllers\VistaReportController;
use App\Http\Controllers\MailingController;
use App\Http\Controllers\MailingListController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\ImpostazioniController;
use App\Http\Controllers\Admin\UtenteController;
use App\Http\Controllers\Admin\RuoloController;
use App\Http\Controllers\Admin\ActivityLogController;
use App\Http\Controllers\Admin\EmailSettingsController;
use App\Http\Controllers\EmailController;
use App\Http\Middleware\AdminOnly;
use App\Http\Middleware\CheckPermission;

// Landing
Route::get('/', function () {
    if (auth()->check()) {
        return redirect()->route('dashboard');
    }
    return redirect()->route('login');
});

// Auth
Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [AuthController::class, 'login']);
    Route::get('/register', [AuthController::class, 'showRegisterForm'])->name('register');
    Route::post('/register', [AuthController::class, 'register']);
});

Route::post('/logout', [AuthController::class, 'logout'])->name('logout')->middleware('auth');

// Dashboard
Route::get('/dashboard', [HomeController::class, 'index'])->name('dashboard')->middleware('auth');

// Resource routes
// Individui specific routes (MUST be before resource route)
Route::get('individui/email-list', [IndividuoController::class, 'emailList'])->middleware('auth');
Route::get('individui/all', [IndividuoController::class, 'allIndividui'])->middleware('auth');
Route::get('individui/export', [IndividuoController::class, 'exportCSV'])->middleware('auth')->name('individui.export');

Route::get('individui/import', [IndividuoController::class, 'import'])->middleware('auth')->name('individui.import');
Route::post('individui/import', [IndividuoController::class, 'importStore'])->middleware('auth')->name('individui.import.store');
Route::get('individui/template', [IndividuoController::class, 'downloadTemplate'])->middleware('auth')->name('individui.template');
Route::get('individui/report/stampa', [IndividuoController::class, 'reportStampa'])->middleware('auth')->name('individui.report.stampa');
Route::get('individui/{individuo}/report', [IndividuoController::class, 'report'])->middleware('auth')->name('individui.report');
Route::get('individui/{individuo}/collegati', [IndividuoController::class, 'elementiCollegati'])->middleware('auth');
Route::post('individui/{individuo}/elimina', [IndividuoController::class, 'elimina'])->middleware('auth');
Route::post('individui/mass-elimina', [IndividuoController::class, 'massElimina'])->middleware('auth');
Route::post('individui/{individuo}/avatar', [IndividuoController::class, 'uploadAvatar'])->middleware('auth');
Route::get('individui/{individuo}/avatar', [IndividuoController::class, 'getAvatar'])->middleware('auth');
Route::delete('individui/{individuo}/avatar', [IndividuoController::class, 'deleteAvatar'])->middleware('auth');
Route::resource('individui', IndividuoController::class)->middleware('auth');

// Gruppo Membri routes (before resource to avoid conflict)
Route::post('gruppi/{gruppo}/membri', [GruppoMembroController::class, 'store'])->middleware('auth');
Route::put('gruppi/{gruppo}/membri/{individuo}', [GruppoMembroController::class, 'update'])->middleware('auth');
Route::patch('gruppi/{gruppo}/membri/{individuo}', [GruppoMembroController::class, 'update'])->middleware('auth');
Route::delete('gruppi/{gruppo}/membri/{individuo}', [GruppoMembroController::class, 'destroy'])->middleware('auth');

// Gruppi specific routes (MUST be before resource route)
Route::get('gruppi/all', [GruppoController::class, 'allGruppi'])->middleware('auth');
Route::get('gruppi/{gruppo}/individui-email', [GruppoController::class, 'individuiEmail'])->middleware('auth');

Route::post('gruppi/{gruppo}/avatar', [\App\Http\Controllers\GruppoAvatarController::class, 'store'])->middleware('auth');
Route::get('gruppi/{gruppo}/avatar-file', [\App\Http\Controllers\GruppoAvatarController::class, 'show'])->middleware('auth');
Route::delete('gruppi/{gruppo}/avatar', [\App\Http\Controllers\GruppoAvatarController::class, 'destroy'])->middleware('auth');

Route::resource('gruppi', GruppoController::class)->middleware('auth');
Route::get('eventi/calendar', [EventoController::class, 'calendar'])->name('eventi.calendar')->middleware('auth');
Route::get('eventi/calendar/events', [EventoController::class, 'calendarEvents'])->name('eventi.calendar.events')->middleware('auth');
Route::get('eventi/{evento}/export-ics', [EventoController::class, 'exportIcs'])->name('eventi.export-ics')->middleware('auth');
Route::post('eventi/export-ics', [EventoController::class, 'exportSelectedIcs'])->name('eventi.export-selected-ics')->middleware('auth');
Route::post('eventi/mass-elimina', [EventoController::class, 'massElimina'])->middleware('auth');
Route::resource('eventi', EventoController::class)->middleware('auth');
Route::post('eventi/{evento}/documenti', [EventoDocumentoController::class, 'store'])->middleware('auth');
Route::delete('eventi/{evento}/documenti/{documento}', [EventoDocumentoController::class, 'destroy'])->middleware('auth');
Route::delete('contatti/{contatto}', [ContattoController::class, 'destroy'])->name('contatti.destroy')->middleware('auth');
Route::put('contatti/{contatto}', [ContattoController::class, 'update'])->name('contatti.update')->middleware('auth');
Route::patch('contatti/{contatto}', [ContattoController::class, 'update'])->middleware('auth');

Route::post('individui/{individuo}/gruppi', [GruppoIndividuoController::class, 'store'])->middleware('auth');
Route::put('individui/{individuo}/gruppi/{gruppo}', [GruppoIndividuoController::class, 'update'])->middleware('auth');
Route::patch('individui/{individuo}/gruppi/{gruppo}', [GruppoIndividuoController::class, 'update'])->middleware('auth');
Route::delete('individui/{individuo}/gruppi/{gruppo}', [GruppoIndividuoController::class, 'destroy'])->middleware('auth');

Route::post('gruppi/save-vista', [GruppoController::class, 'saveVista'])->middleware('auth')->name('gruppi.save-vista');
Route::delete('gruppi/vista/{vista}', [GruppoController::class, 'deleteVista'])->middleware('auth')->name('gruppi.delete-vista');

Route::get('documenti', [DocumentoController::class, 'index'])->name('documenti.index')->middleware('auth');
Route::post('documenti', [DocumentoController::class, 'store'])->middleware('auth');

Route::post('documenti/mass-destroy', [DocumentoController::class, 'massDestroy'])->middleware('auth');
Route::post('documenti/mass-update', [DocumentoController::class, 'massUpdate'])->middleware('auth');
Route::post('documenti/mass-associate', [DocumentoController::class, 'massAssociate'])->middleware('auth');
Route::post('documenti/check-links', [DocumentoController::class, 'checkMassLinks'])->middleware('auth');

Route::get('documenti/options', [DocumentoController::class, 'options'])->middleware('auth');
Route::get('documenti/{documento}/check-links', [DocumentoController::class, 'checkLinks'])->middleware('auth');
Route::get('documenti/{documento}/edit', [DocumentoController::class, 'edit'])->middleware('auth');
Route::put('documenti/{documento}', [DocumentoController::class, 'update'])->middleware('auth');
Route::get('documenti/{documento}/download', [DocumentoController::class, 'download'])->middleware('auth');
Route::get('documenti/{documento}/preview', [DocumentoController::class, 'preview'])->middleware('auth');
Route::delete('documenti/{documento}', [DocumentoController::class, 'destroy'])->middleware('auth');

Route::get('viste', [VistaReportController::class, 'index'])->name('viste.index')->middleware('auth');
Route::post('viste', [VistaReportController::class, 'store'])->middleware('auth');
Route::get('viste/{vistaReport}/json', [VistaReportController::class, 'showJson'])->middleware('auth');
Route::put('viste/{vistaReport}', [VistaReportController::class, 'update'])->middleware('auth');
Route::patch('viste/{vistaReport}/default', [VistaReportController::class, 'setDefault'])->middleware('auth');
Route::delete('viste/{vistaReport}', [VistaReportController::class, 'destroy'])->middleware('auth');

Route::get('report', [ReportController::class, 'index'])->name('report.index')->middleware('auth');
Route::get('report/run', [ReportController::class, 'run'])->name('report.run')->middleware('auth');
Route::get('report/print-preview', [ReportController::class, 'printPreview'])->name('report.print-preview')->middleware('auth');
Route::get('report/export', [ReportController::class, 'exportCSV'])->name('report.export')->middleware('auth');
Route::get('report/custom/{id}', [ReportController::class, 'runCustom'])->name('report.run-custom')->middleware('auth');
Route::post('report/custom', [ReportController::class, 'storeCustom'])->name('report.store-custom')->middleware('auth');
Route::delete('report/custom/{id}', [ReportController::class, 'destroyCustom'])->name('report.destroy-custom')->middleware('auth');

Route::get('mailing/nuovo', [MailingController::class, 'nuovo'])->middleware('auth')->name('mailing.nuovo');
Route::post('mailing/invia', [MailingController::class, 'invia'])->middleware('auth')->name('mailing.invia');
Route::get('mailing/invio', [MailingController::class, 'invio'])->middleware('auth')->name('mailing.invio');
Route::post('mailing/invio/elabora', [MailingController::class, 'invioElabora'])->middleware('auth')->name('mailing.invio.elabora');

Route::resource('mailing-liste', MailingListController::class)->middleware('auth');
Route::post('mailing-liste/create-from-individui', [MailingListController::class, 'createFromIndividui'])->middleware('auth');
Route::post('mailing-liste/mass-elimina', [MailingListController::class, 'massElimina'])->middleware('auth');

Route::get('/impostazioni', [ImpostazioniController::class, 'index'])->middleware('auth')->name('impostazioni.index');
Route::post('/impostazioni/tipologie', [ImpostazioniController::class, 'tipologieStore'])->middleware('auth')->name('impostazioni.tipologie.store');
Route::put('/impostazioni/tipologie/{id}', [ImpostazioniController::class, 'tipologieUpdate'])->middleware('auth')->name('impostazioni.tipologie.update');
Route::delete('/impostazioni/tipologie/{id}', [ImpostazioniController::class, 'tipologieDestroy'])->middleware('auth')->name('impostazioni.tipologie.destroy');
Route::post('/impostazioni/tipologie/reorder', [ImpostazioniController::class, 'tipologieReorder'])->middleware('auth')->name('impostazioni.tipologie.reorder');
Route::post('/impostazioni/ruoli', [ImpostazioniController::class, 'ruoliStore'])->middleware('auth')->name('impostazioni.ruoli.store');
Route::put('/impostazioni/ruoli/{id}', [ImpostazioniController::class, 'ruoliUpdate'])->middleware('auth')->name('impostazioni.ruoli.update');
Route::delete('/impostazioni/ruoli/{id}', [ImpostazioniController::class, 'ruoliDestroy'])->middleware('auth')->name('impostazioni.ruoli.destroy');
Route::post('/impostazioni/ruoli/reorder', [ImpostazioniController::class, 'ruoliReorder'])->middleware('auth')->name('impostazioni.ruoli.reorder');
Route::post('/impostazioni/tipologie-eventi', [ImpostazioniController::class, 'tipologieEventiStore'])->middleware('auth')->name('impostazioni.tipologie-eventi.store');
Route::put('/impostazioni/tipologie-eventi/{id}', [ImpostazioniController::class, 'tipologieEventiUpdate'])->middleware('auth')->name('impostazioni.tipologie-eventi.update');
Route::delete('/impostazioni/tipologie-eventi/{id}', [ImpostazioniController::class, 'tipologieEventiDestroy'])->middleware('auth')->name('impostazioni.tipologie-eventi.destroy');
Route::post('/impostazioni/tipologie-eventi/reorder', [ImpostazioniController::class, 'tipologieEventiReorder'])->middleware('auth')->name('impostazioni.tipologie-eventi.reorder');
Route::post('/impostazioni/logo', [ImpostazioniController::class, 'uploadLogo'])->middleware('auth')->name('impostazioni.logo.upload');
Route::post('/impostazioni/logo/remove', [ImpostazioniController::class, 'removeLogo'])->middleware('auth')->name('impostazioni.logo.remove');
Route::post('/impostazioni/app-settings', [ImpostazioniController::class, 'saveAppSettings'])->middleware('auth')->name('impostazioni.app-settings.save');

Route::get('/impostazioni/email', [EmailSettingsController::class, 'index'])->middleware('auth')->name('impostazioni.email.index');
Route::post('/impostazioni/email', [EmailSettingsController::class, 'save'])->middleware('auth')->name('impostazioni.email.save');
Route::post('/impostazioni/email/test', [EmailSettingsController::class, 'testConnection'])->middleware('auth')->name('impostazioni.email.test');
    Route::post('/impostazioni/email/test-smtp', [EmailSettingsController::class, 'testSmtp'])->middleware('auth')->name('impostazioni.email.testSmtp');
Route::get('/impostazioni/email/sync', [EmailSettingsController::class, 'syncNow'])->middleware('auth')->name('impostazioni.email.sync');
Route::post('/impostazioni/sender', [EmailSettingsController::class, 'senderStore'])->middleware('auth')->name('impostazioni.sender.store');
Route::put('/impostazioni/sender/{id}', [EmailSettingsController::class, 'senderUpdate'])->middleware('auth')->name('impostazioni.sender.update');
Route::delete('/impostazioni/sender/{id}', [EmailSettingsController::class, 'senderDestroy'])->middleware('auth')->name('impostazioni.sender.destroy');
Route::post('/impostazioni/sender/{id}/test', [EmailSettingsController::class, 'senderTestSmtp'])->middleware('auth')->name('impostazioni.sender.test');

Route::get('/email/{folder?}', [EmailController::class, 'index'])->middleware('auth')->name('email.index')->defaults('folder', 'inbox');
Route::get('/email/{folder}/{id}', [EmailController::class, 'show'])->middleware('auth')->name('email.show');
Route::get('/email/compose', [EmailController::class, 'compose'])->middleware('auth')->name('email.compose');
Route::post('/email/send', [EmailController::class, 'send'])->middleware('auth')->name('email.send');
Route::post('/email/draft', [EmailController::class, 'storeDraft'])->middleware('auth')->name('email.draft');
Route::post('/email/{id}/read', [EmailController::class, 'markRead'])->middleware('auth')->name('email.read');
Route::post('/email/{id}/star', [EmailController::class, 'toggleStar'])->middleware('auth')->name('email.star');
Route::delete('/email/{id}', [EmailController::class, 'destroy'])->middleware('auth')->name('email.destroy');
Route::post('/email/{id}/attachment/{attachmentId}/save', [EmailController::class, 'saveAttachmentToDocumenti'])->middleware('auth')->name('email.attachment.save');
Route::get('/email/attachment/{id}/download', [EmailController::class, 'downloadAttachment'])->middleware('auth')->name('email.attachment.download');
Route::post('/email/sync/quick', [EmailController::class, 'quickSync'])->middleware('auth')->name('email.sync.quick');
Route::post('/email/bulk', [EmailController::class, 'bulkAction'])->middleware('auth')->name('email.bulk');

// Admin routes (admin only)
Route::prefix('admin')->middleware(['auth', AdminOnly::class])->group(function () {
    Route::get('/utenti', [UtenteController::class, 'index'])->name('admin.utenti.index');
    Route::get('/utenti/create', [UtenteController::class, 'create'])->name('admin.utenti.create');
    Route::post('/utenti', [UtenteController::class, 'store'])->name('admin.utenti.store');
    Route::get('/utenti/{id}/edit', [UtenteController::class, 'edit'])->name('admin.utenti.edit');
    Route::put('/utenti/{id}', [UtenteController::class, 'update'])->name('admin.utenti.update');
    Route::delete('/utenti/{id}', [UtenteController::class, 'destroy'])->name('admin.utenti.destroy');

    Route::get('/ruoli', [RuoloController::class, 'index'])->name('admin.ruoli.index');
    Route::get('/ruoli/create', [RuoloController::class, 'create'])->name('admin.ruoli.create');
    Route::post('/ruoli', [RuoloController::class, 'store'])->name('admin.ruoli.store');
    Route::get('/ruoli/{id}/edit', [RuoloController::class, 'edit'])->name('admin.ruoli.edit');
    Route::put('/ruoli/{id}', [RuoloController::class, 'update'])->name('admin.ruoli.update');
    Route::delete('/ruoli/{id}', [RuoloController::class, 'destroy'])->name('admin.ruoli.destroy');

    Route::get('/activity-logs', [ActivityLogController::class, 'index'])->name('admin.activity-logs.index');
});