<?php

namespace App\Providers\Filament;

use App\Filament\Widgets\DokployPlatformHealth;
use App\Filament\Widgets\PlatformStatsWidget;
use App\Filament\Widgets\RevenueTrendWidget;
use App\Filament\Widgets\TopTenantsByRevenue;
use App\Filament\Widgets\TopTenantsByUploads;
use Boquizo\FilamentLogViewer\FilamentLogViewerPlugin;
use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Navigation\NavigationGroup;
use Filament\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Colors\Color;
use Filament\Support\Icons\Heroicon;
use Filament\Widgets;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;

class SuperAdminPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {

        return $panel
            ->default()
            ->id('superadmin')
            ->path('super-admin')
            ->topNavigation()
            ->colors([
                'primary' => Color::Pink,
            ])
            ->plugins([
                FilamentLogViewerPlugin::make()
                    ->navigationGroup(__('admin.nav.infrastructure'))
                    ->navigationLabel('Log Viewer')
                    ->navigationIcon(Heroicon::OutlinedDocumentText)
                    ->navigationSort(20),
            ])
            ->navigationGroups([
                NavigationGroup::make()
                    ->label(__('admin.nav.curation')),
                NavigationGroup::make()
                    ->label(__('admin.nav.events')),
                NavigationGroup::make()
                    ->label(__('admin.nav.tenants')),
                NavigationGroup::make()
                    ->label(__('admin.nav.billing')),
                NavigationGroup::make()
                    ->label(__('admin.nav.feedback_support')),
                NavigationGroup::make()
                    ->label(__('admin.nav.platform')),
                NavigationGroup::make()
                    ->label(__('admin.nav.commercial')),
                NavigationGroup::make()
                    ->label(__('admin.nav.infrastructure')),
                NavigationGroup::make()
                    ->label(__('admin.nav.storage')),
                NavigationGroup::make()
                    ->label(__('admin.nav.content')),
                NavigationGroup::make()
                    ->label(__('admin.nav.branding')),
            ])
            ->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
            ->discoverResources(in: app_path('Filament/Blog/Resources'), for: 'App\\Filament\\Blog\\Resources')
            ->discoverPages(in: app_path('Filament/SuperAdmin/Pages'), for: 'App\\Filament\\SuperAdmin\\Pages')
            ->discoverClusters(in: app_path('Filament/Clusters'), for: 'App\\Filament\\Clusters')
            ->pages([
                Pages\Dashboard::class,
            ])
            ->login(\App\Filament\Pages\Auth\Login::class)
            /*->plugin(
                BlogPlugin::make()
            )*/
            ->profile()
            ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
            ->widgets([
                Widgets\AccountWidget::class,
                Widgets\FilamentInfoWidget::class,
                RevenueTrendWidget::class,
                PlatformStatsWidget::class,
                \App\Filament\Widgets\CouponUsageWidget::class,
                TopTenantsByRevenue::class,
                TopTenantsByUploads::class,
                \App\Filament\Widgets\StorageCapacityWidget::class,
                DokployPlatformHealth::class,
            ])
            ->middleware([
                EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
                AuthenticateSession::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
            ])
            ->authMiddleware([
                Authenticate::class,
            ])
            ->authGuard('super_admin');

        // SuperAdmin-Zugriff durch custom Middleware, globale Sichtbarkeit ohne Tenant-Isolation
        // Blog-Resources werden durch das Plugin-ServiceProvider automatisch registriert
    }
}
