<?php

declare(strict_types=1);

use App\Http\Controllers\GenDocController;
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains;

use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\CategorieController;
use App\Http\Controllers\CondominioController;
use App\Http\Controllers\ContatoreEnElController;
use App\Http\Controllers\ContatoreGasController;
use App\Http\Controllers\DocumentiController;
use App\Http\Controllers\MovimentiController;
use App\Http\Controllers\RigaProgettoController;
use App\Http\Controllers\TagController;
use App\Http\Controllers\FullCalenderController;
use App\Http\Controllers\AutoController;
use App\Http\Controllers\AnagraficaController;
use App\Http\Controllers\Utenti;
use App\Http\Controllers\ProgettiController;
use App\Http\Controllers\TaskController;
use App\Http\Controllers\AvvisoController;
use App\Http\Controllers\ContrattiController;
use App\Http\Controllers\TodolistController;
use App\Http\Controllers\ContoController;
use App\Mail\myTestEmail;

// API
use Illuminate\Http\Request;
// use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\MovimentiController as ApiMovimentiController;
use App\Http\Controllers\Api\CategorieController as ApiCategorieController;
use App\Http\Controllers\Api\ContoController as ApiContoController;


/*
|--------------------------------------------------------------------------
| Tenant Routes
|--------------------------------------------------------------------------
|
| Here you can register the tenant routes for your application.
| These routes are loaded by the TenantRouteServiceProvider.
|
| Feel free to customize them however you want. Good luck!
|
*/

Route::middleware([
    'web',
    InitializeTenancyByDomain::class,
    PreventAccessFromCentralDomains::class,
])->group(function () {

    Route::get('/', function () {
        return view('welcome');
    });


    Auth::routes(['register' => false]); // Production only
    //   Auth::routes();    //development mode
    Route::get('/calendar', [TagController::class,'calendartest'])->name('calendar');
    // Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
    Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
    Route::get('/test', function(){ return view('test');});
    Route::get('/test/ha-energy',[App\Http\Controllers\HomeAssistantAPIController::class,'getContatore']);

});

Route::middleware([
    'web',
    'auth',
    InitializeTenancyByDomain::class,
    PreventAccessFromCentralDomains::class,
])->group(function () {
    /*Route::get('admin', function () {
        dd(\App\Models\User::all());
        return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id');
    });*/
        Route::get('admin/', [MovimentiController::class,'dashboard']);
        Route::get('changepassword',function(){return view('auth.change-password');})->name('changePassword');
        Route::post('changepassword', [Utenti::class,'changePassword']);
        Route::get('logout', function(){ Auth::logout(); return redirect('login'); })->name('logout');

        // MOVIMENTI
        Route::group(['middleware'=>['permission:conti']], function(){
            Route::post('admin/movimenti/spesa',[MovimentiController::class,'insMovimentiSpesa']);
            Route::post('admin/movimenti/entrata',[MovimentiController::class,'insMovimentiEntrata']);
            Route::get('admin/movimenti',[MovimentiController::class,'listMovimenti'])->name('movimenti');
            Route::get('admin/movimenti/export',[MovimentiController::class,'exportMovimenti'])->name('export');
            Route::get('admin/movimenti/resoconto',[MovimentiController::class,'resocontoMovimenti'])->name('resoconto');
            Route::get('admin/movimenti/modify/{id}',[MovimentiController::class,'updateMovimenti'])->whereNumber('id');
            Route::post('admin/movimenti/modify',[MovimentiController::class,'updatePostMovimenti']);
            Route::get('admin/movimenti/delete',[MovimentiController::class,'deleteMovimenti']);
            Route::get('admin/movimenti/reportbudget/{anno?}',[MovimentiController::class,'reportCategorieAnno'])->name('budget')->whereNumber('anno');
            Route::post('admin/movimenti/reportbudget/{anno?}',[MovimentiController::class,'manageRedirect']);
            Route::get('admin/movimenti/reportbudgetxls',[MovimentiController::class,'reportCategorieAnnoXLS'])->name('budgetxls')->whereNumber('anno');
            Route::get('admin/movimenti/filter/tags',[MovimentiController::class,'filterByTag']);
            Route::get('admin/movimenti/report/movimenti_categoria', [MovimentiController::class,'listMovPerCateg']);
            Route::get('admin/movimenti/report/movimentibycat', [MovimentiController::class,'listMovbyCat']);
            // Route::get('admin/movimenti/docs', [DocumentiController::class,'fileForm'])->name('documenti');
            // Route::post('admin/movimenti/docs', [DocumentiController::class,'storeFile']);
            Route::get('admin/movimenti/docs', [App\Http\Controllers\GenDocController::class,'fileForm'])->name('documenti');
            Route::post('admin/movimenti/docs', [App\Http\Controllers\GenDocController::class,'storeFile']);
            // {issue #5}
            // Route::get('movimenti/docs', [DocumentiController::class,'fileMovimentiForm'])->name('documenti');

            Route::get('admin/movimenti/import', [MovimentiController::class,'importFile'])->name('importING');
            Route::post('admin/movimenti/import', [MovimentiController::class,'importEC_ING']);
            Route::get('admin/movimenti/importcr', [MovimentiController::class,'importFileCR'])->name('importCR');
            Route::post('admin/movimenti/importcr', [MovimentiController::class,'importEC_CR']);
            Route::get('/admin/movimenti/giroconto', [MovimentiController::class,'giroconto'])->name('giroconto');
            Route::post('/admin/movimenti/giroconto', [MovimentiController::class,'girocontoPost']);

            Route::resource('admin/conti', ContoController::class);


        // CATEGORIE
            Route::get('admin/categorie', [CategorieController::class,'listCategorie'])->name('categorie');
            Route::post('admin/categorie', [CategorieController::class,'insCategorie']);
            Route::get('admin/categorie/delete', [CategorieController::class,'deleteCategorie']);
            Route::get('admin/categorie/modify/{id}', [CategorieController::class,'updateCategorie'])->whereNumber('id');
            Route::post('admin/categorie/modify', [CategorieController::class,'updatePostCategorie']);

        // Richiami di servizio da jquery
            Route::get('admin/service/catlist', [CategorieController::class,'apiList']);
            Route::get('admin/service/taglist', [TagController::class,'apiList']);
            Route::get('admin/service/rolesList', [Utenti::class,'listRoles']);
            Route::get('admin/service/catlistSpesa', [CategorieController::class,'apiListSpesa']);
            Route::get('admin/service/catlistEntrata', [CategorieController::class,'apiListEntrata']);
            Route::get('admin/service/contolist', [ContoController::class,'apiListConto']);

        // TAGS
            Route::get('admin/tags', [TagController::class,'listTags'])->name('tags');
            Route::post('admin/tags', [TagController::class,'insTags']);
            Route::get('admin/tags/modify/{id}', [TagController::class,'updateTag'])->whereNumber('id');
            Route::post('admin/tags/modify', [TagController::class,'updatePostTag']);
            Route::get('admin/tags/delete/{id}',[TagController::class,'deleteTag'])->whereNumber('id');
        });
        // CONSUMI
        Route::group(['middleware'=>['permission:consumi']], function(){
            Route::get('admin/consumi/gas', [ContatoreGasController::class,'listLettureGas'])->name('gas');
            Route::post('admin/consumi/gas', [ContatoreGasController::class,'insLettureGas']);
            Route::get('admin/consumi/enel', [ContatoreEnElController::class,'listLettureEnel'])->name('enel');
            Route::post('admin/consumi/enel', [ContatoreEnElController::class,'insLettureEnel']);
        });

        // AUTOMOBILI
        Route::group(['middleware'=>['permission:automobili']], function(){
            Route::get('admin/auto', [AutoController::class, 'index'])->name('auto_list');
            Route::get('admin/auto/new', [AutoController::class, 'newAuto'])->name('auto_new');
            Route::post('admin/auto/new', [AutoController::class, 'saveAuto'])->name('auto_save');
            Route::get('admin/auto/getAuto/{id}', [AutoController::class, 'getAutoById'])->whereNumber('id');
            Route::post('admin/auto/modify', [AutoController::class, 'udateAuto']);
            Route::get('admin/auto/delete', [AutoController::class, 'delAuto']);
            Route::get('admin/auto/detail', [AutoController::class, 'getAutoDetails']);
            Route::get('admin/auto/rifornimento/{id}', [AutoController::class, 'rifornimentoAuto'])->name('auto_rifornimento')->whereNumber('id');
            Route::post('admin/auto/rifornimento', [AutoController::class, 'saveRifornimento']);
            Route::get('admin/auto/revisione', [AutoController::class, 'revisioneAuto']);
            Route::post('admin/auto/revisione', [AutoController::class, 'saveRevisione']);
            Route::get('admin/auto/manutenzione', [AutoController::class, 'manutenzioneAuto']);
            Route::post('admin/auto/manutenzione', [AutoController::class, 'saveManutenzione']);
            Route::get('admin/auto/accessori', [AutoController::class, 'accessoriAuto']);
            Route::post('admin/auto/accessori', [AutoController::class, 'saveAccessori']);
            Route::get('admin/auto/operazioni', [AutoController::class, 'getOperazioni']);
            Route::get('admin/auto/operazioni/pdf', [AutoController::class, 'exportPdfOperazioni']);
        });
        // CONTATTI
        Route::group(['middleware'=>['permission:contatti']], function(){
            Route::get('admin/contatti', [AnagraficaController::class, 'listContact'])->name('contatti');
            Route::get('admin/contatti/new', [AnagraficaController::class, 'newContact'])->name('newContact');
            Route::post('admin/contatti/new', [AnagraficaController::class, 'insContact']);
            Route::get('admin/contatti/modifica', [AnagraficaController::class, 'modifica']);
            Route::get('admin/contatti/scheda', [AnagraficaController::class, 'getScheda']);
            Route::get('admin/contatti/addOther', [AnagraficaController::class, 'insOtherContact']);
            Route::post('admin/contatti/addOther', [AnagraficaController::class, 'saveOtherContact']);

        });

        // GRUPPI E PERMESSI
        Route::group(['middleware'=>['permission:amministrazione']], function(){

            Route::get('admin/users/new',[Utenti::class,'addUser']);
            Route::post('admin/users/new',[Utenti::class,'createUser']);
            Route::get('admin/users/roles',[Utenti::class,'listRoles']);
            Route::get('admin/users/delete/{id}',[Utenti::class,'deleteUser']);
            Route::get('admin/users/givepermission',[Utenti::class,'givePermissionToUser'])->name('permessi');
            Route::post('admin/users/givepermission',[Utenti::class,'assignPermission']);
            Route::get('admin/users/giverole',[Utenti::class,'giveRoleToUser']);
            Route::post('admin/users/giverole',[Utenti::class,'assignRole']);
            Route::get('admin/backup', [App\Http\Controllers\HomeController::class,'backupTenant']);
        });

        // PROGETTI
        Route::group(['middleware'=>['permission:progetti']], function(){
            Route::get('admin/progetti', [ProgettiController::class, 'listaProgetto'])->name('progetti');
            Route::post('admin/progetti/new', [ProgettiController::class, 'salvaProgetto']);
            Route::get('admin/progetti/new', [ProgettiController::class, 'nuovoProgetto'])->name('nuovoProgetto');
            Route::get('admin/progetti/delete',[ProgettiController::class, 'deleteProgetto']);
            Route::get('admin/progetti/delete_row/{id_row}/return/{id_prog}',[RigaProgettoController::class, 'deleterow']);
            Route::get('admin/progetti/detail/{id}', [ProgettiController::class, 'dettaglioProgetto'])->name('detail')->whereNumber('id');
            Route::post('admin/progetti/detail/{id}', [RigaProgettoController::class, 'inserisciRiga'])->whereNumber('id');
            Route::get('admin/progetti/detail/edit/{id}', [RigaProgettoController::class, 'editRiga'])->whereNumber('id');;
            Route::post('admin/progetti/rigaupdate', [RigaProgettoController::class, 'updateRiga']);
            Route::get('admin/progetti/coordinatori', [ProgettiController::class, 'getCoordinatori']);
            Route::get('admin/progetti/close',[ProgettiController::class, 'chiudiProgetto']);
            Route::get('admin/progetti/reopen',[ProgettiController::class, 'riapriProgetto']);
            Route::get('admin/progetti/print',[ProgettiController::class,'stampaPDFProgetto']);
            Route::get('admin/progetti/docs', [ProgettiController::class, 'documentoProgetto']);
            Route::post('admin/progetti/docs', [App\Http\Controllers\GenDocController::class,'storeFile']);
            Route::get('admin/progetti/getProgettoById',[ProgettiController::class, 'getProgettobyId']);
            Route::post('admin/progetti/modificaProgetto',[ProgettiController::class,'modificaProgetto']);
        });

        // TASKS
        Route::group(['middleware'=>['permission:tasks']], function(){
            Route::get('admin/tasks', [TaskController::class, 'Task'])->name('tasks');
            Route::post('admin/task/new',[TaskController::class, 'newTask']);
            Route::get('admin/service/getUsers', [Utenti::class,'getUsers']);
            Route::resource('admin/todolists', TodolistController::class);
        });


        // CONTRATTI
        Route::group(['middleware'=>['permission:contratti']], function(){
            Route::get('admin/contratti', [ContrattiController::class, 'Contratti'])->name('contratti');
            Route::post('admin/contratti', [ContrattiController::class, 'newContratto']);
            Route::get('admin/contratti/{id}', [ContrattiController::class, 'schedaContratto'])->name('contrattodetail');
            Route::get('admin/contrattidoc/doc', [ContrattiController::class, 'documentDelete'])->name('delcontratti');
        });

        Route::group(['middleware'=>['permission:avvisi']], function(){
            Route::post('admin/avvisi/new',[AvvisoController::class,'saveAvviso'])->name('newAvviso');
        });
        // -- ONLY FOR TEST -- TO BE REMOVED //

        // DOCUMENTI GENERICI
        Route::group(['middleware'=>['permission:documenti']], function(){
            Route::get('admin/documenti', [DocumentiController::class, 'listaDocumenti'])->name('folders');
            Route::post('admin/documenti', [DocumentiController::class, 'newFolder']);
            Route::get('admin/doc_delete', [GenDocController::class,'deleteFile']);
        });

        // APPUNTAMENTI
        Route::group(['middleware'=>['permission:appuntamenti']], function(){
            Route::get('admin/test/calendar',[FullCalenderController::class,'showDemo'])->name('calendario');
            Route::post('admin/test/calendar',[FullCalenderController::class, 'saveEventToGoogle']);
        });

        // MEETING ROOM
        Route::group(['middleware'=>['permission:meetingroom']], function(){
            Route::get('admin/meeting', function() { return view('meetings.meeting'); })->name('meeting');
        });



// API
});
Route::middleware([
    'api',
    InitializeTenancyByDomain::class,
    PreventAccessFromCentralDomains::class,
])->group(function (){
Route::post('/api/login', [Utenti::class,'login'])->withoutMiddleware([\App\Http\Middleware\VerifyCsrfToken::class]);

Route::middleware('auth:sanctum')->group(function () {
    Route::post('/api/movimenti/spesa', [ApiMovimentiController::class, 'insMovimentiSpesa']);
    Route::post('/api/movimenti/entrata', [ApiMovimentiController::class, 'insMovimentiEntrata']);
    Route::get('/api/movimenti', [ApiMovimentiController::class, 'getList']);

    Route::get('/api/categorie', [ApiCategorieController::class, 'listCategorie']);
    Route::post('/api/categorie', [ApiCategorieController::class, 'insCategorie']);
    Route::put('/api/categorie/{id}', [ApiCategorieController::class, 'updateCategorie']);
    Route::delete('/api/categorie/{id}', [ApiCategorieController::class, 'deleteCategorie']);

    Route::get('/api/conti', [ApiContoController::class, 'listConti']);
    Route::post('/api/conti', [ApiContoController::class, 'insConto']);
    Route::put('/api/conti/{id}', [ApiContoController::class, 'updateConto']);
    Route::delete('/api/conti/{id}', [ApiContoController::class, 'deleteConto']);


});
});
// /API
