<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;
use ImovelZapAi\SuperAdmin\Infrastructure\Http\Controllers\Web\PlatformAiController;
use ImovelZapAi\SuperAdmin\Infrastructure\Http\Controllers\Web\PlatformAuthController;
use ImovelZapAi\SuperAdmin\Infrastructure\Http\Controllers\Web\PlatformBillingController;
use ImovelZapAi\SuperAdmin\Infrastructure\Http\Controllers\Web\PlatformDashboardController;
use ImovelZapAi\SuperAdmin\Infrastructure\Http\Controllers\Web\PlatformHealthController;
use ImovelZapAi\SuperAdmin\Infrastructure\Http\Controllers\Web\PlatformIncidentController;
use ImovelZapAi\SuperAdmin\Infrastructure\Http\Controllers\Web\PlatformSettingsController;
use ImovelZapAi\SuperAdmin\Infrastructure\Http\Controllers\Web\PlatformSupportController;
use ImovelZapAi\SuperAdmin\Infrastructure\Http\Controllers\Web\PlatformTenantController;

Route::prefix('super-admin')->name('super-admin.')->group(function (): void {
    Route::middleware(['platform.ip'])->group(function (): void {
        Route::get('login', [PlatformAuthController::class, 'showLogin'])->name('login');
        Route::post('login', [PlatformAuthController::class, 'login'])->name('login.submit');

        Route::middleware(['platform.auth', 'throttle:platform'])->group(function (): void {
            Route::post('logout', [PlatformAuthController::class, 'logout'])->name('logout');
            Route::get('mfa', [PlatformAuthController::class, 'showMfa'])->name('mfa.challenge');
            Route::post('mfa', [PlatformAuthController::class, 'verifyMfa'])->name('mfa.verify');

            Route::middleware(['platform.mfa'])->group(function (): void {
                Route::get('/', PlatformDashboardController::class)->name('dashboard');

                Route::middleware(['platform.role:super_admin,support'])->group(function (): void {
                    Route::get('clientes', [PlatformTenantController::class, 'index'])->name('tenants.index');
                    Route::get('clientes/criar', [PlatformTenantController::class, 'create'])->name('tenants.create');
                    Route::post('clientes', [PlatformTenantController::class, 'store'])->name('tenants.store');
                    Route::get('clientes/{tenant}', [PlatformTenantController::class, 'show'])->name('tenants.show');
                    Route::post('clientes/{tenant}/status', [PlatformTenantController::class, 'updateStatus'])->name('tenants.status');
                    Route::post('clientes/{tenant}/plano', [PlatformTenantController::class, 'updatePlan'])->name('tenants.plan');
                });

                Route::middleware(['platform.role:super_admin,finance,auditor'])->group(function (): void {
                    Route::get('planos', [PlatformBillingController::class, 'plans'])->name('plans.index');
                    Route::get('assinaturas', [PlatformBillingController::class, 'subscriptions'])->name('billing.subscriptions');
                });

                Route::middleware(['platform.role:super_admin,finance'])->group(function (): void {
                    Route::post('planos/{plan}', [PlatformBillingController::class, 'updatePlan'])->name('plans.update');
                });

                Route::middleware(['platform.role:super_admin,devops,auditor'])->group(function (): void {
                    Route::get('ia', PlatformAiController::class)->name('ai.index');
                    Route::get('saude', [PlatformHealthController::class, 'index'])->name('health');
                });

                Route::middleware(['platform.role:super_admin,devops'])->group(function (): void {
                    Route::post('saude/atualizar', [PlatformHealthController::class, 'refresh'])->name('health.refresh');
                });

                Route::middleware(['platform.role:super_admin'])->group(function (): void {
                    Route::get('configuracoes', [PlatformSettingsController::class, 'index'])->name('settings');
                    Route::post('configuracoes', [PlatformSettingsController::class, 'update'])->name('settings.update');
                });

                Route::middleware(['platform.role:super_admin,support'])->group(function (): void {
                    Route::get('suporte', [PlatformSupportController::class, 'index'])->name('support');
                    Route::post('suporte', [PlatformSupportController::class, 'store'])->name('support.store');
                    Route::post('suporte/impersonation', [PlatformSupportController::class, 'startImpersonation'])->name('support.impersonation.start');
                    Route::post('suporte/impersonation/encerrar', [PlatformSupportController::class, 'endImpersonation'])->name('support.impersonation.end');
                });

                Route::middleware(['platform.role:super_admin,devops,support'])->group(function (): void {
                    Route::get('incidentes', [PlatformIncidentController::class, 'index'])->name('incidents.index');
                    Route::post('incidentes', [PlatformIncidentController::class, 'store'])->name('incidents.store');
                    Route::post('incidentes/{incident}/status', [PlatformIncidentController::class, 'updateStatus'])->name('incidents.status');
                });
            });
        });
    });
});
