<?php

namespace App\Providers\Filament;

use App\Filament\Blog\Resources\CategoryResource;
use App\Filament\Blog\Resources\PostResource;
use App\Filament\Resources\InfrastructureActionLogs\InfrastructureActionLogResource;
use App\Filament\Resources\LegalPageResource;
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\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Icons\Heroicon;
use Filament\Support\Colors\Color;
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')
            ->colors([
                'primary' => Color::Pink,
            ])
            ->plugins([
                FilamentLogViewerPlugin::make()
                    ->navigationGroup('Platform')
                    ->navigationLabel('Log Viewer')
                    ->navigationIcon(Heroicon::OutlinedDocumentText)
                    ->navigationSort(20),
            ])
            ->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
            ->discoverPages(in: app_path('Filament/SuperAdmin/Pages'), for: 'App\\Filament\\SuperAdmin\\Pages')
            ->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,
            ])
            ->resources([
                \App\Filament\Resources\EventResource::class,
                \App\Filament\Resources\PhotoResource::class,
                \App\Filament\Resources\UserResource::class,
                \App\Filament\Resources\TenantPackageResource::class,
                \App\Filament\Resources\TaskResource::class,
                \App\Filament\Resources\MediaStorageTargetResource::class,
                PostResource::class,
                CategoryResource::class,
                LegalPageResource::class,
                InfrastructureActionLogResource::class,
            ])
            ->pages([
                Pages\Dashboard::class,
                \App\Filament\SuperAdmin\Pages\WatermarkSettingsPage::class,
            ])
            ->authGuard('web');

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