<?php

declare(strict_types=1);


namespace App\Providers\Filament;

use App\Filament\Pages\BlogSettings;
use App\Filament\Resources\Categories\CategoryResource;
use App\Filament\Resources\Comments\CommentResource;
use App\Filament\Resources\Links\LinkResource;
use App\Filament\Resources\Media\MediaResource;
use App\Filament\Resources\Posts\PostResource;
use App\Filament\Resources\Users\UserResource;
use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\AuthenticateSession;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Navigation\NavigationGroup;
use Filament\Pages\Dashboard;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Colors\Color;
use Filament\Widgets\AccountWidget;
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\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;

class AdminPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->default()
            ->id('admin')
            ->path('admin')
            ->login()
            ->colors([
                'primary' => Color::Sky,
            ])
            ->brandName(config('blog.name'))
            ->sidebarWidth('13rem')
            ->maxContentWidth(\Filament\Support\Enums\Width::Full)
            ->userMenuItems([
                // 工具：清空缓存（Livewire action，即时通知）
                \Filament\Actions\Action::make('flush-page-cache')
                    ->label('清空前台缓存')
                    ->icon('heroicon-o-bolt')
                    ->color('warning')
                    ->action(function (): void {
                        \App\Blog\Support\PageCacheMiddleware::flush();
                        \Filament\Notifications\Notification::make()->title('前台页面缓存已清空')->success()->send();
                    }),
                \Filament\Actions\Action::make('flush-all-cache')
                    ->label('清空全部缓存')
                    ->icon('heroicon-o-trash')
                    ->color('danger')
                    ->requiresConfirmation()
                    ->action(function (): void {
                        \App\Blog\Support\PageCacheMiddleware::flush();
                        \App\Models\Setting::flushCache();
                        \Illuminate\Support\Facades\Cache::flush();
                        \Filament\Notifications\Notification::make()->title('全部缓存已清空')->success()->send();
                    }),
                \Filament\Actions\Action::make('blog-settings')
                    ->label('博客设置')
                    ->icon('heroicon-o-cog-6-tooth')
                    ->url(fn (): string => \App\Filament\Pages\BlogSettings::getUrl()),
                \Filament\Actions\Action::make('theme-inject')
                    ->label('主题注入')
                    ->icon('heroicon-o-squares-plus')
                    ->url(fn (): string => \App\Filament\Pages\ThemeInject::getUrl()),
            ])
            ->navigationGroups([
                NavigationGroup::make('内容'),
                NavigationGroup::make('管理'),
                NavigationGroup::make('插件'),
            ])
            ->discoverResources(in: app_path('Filament/Resources'), for: 'App\Filament\Resources')
            ->discoverPages(in: app_path('Filament/Pages'), for: 'App\Filament\Pages')
            ->pages([
                Dashboard::class,
                ...\App\Blog\Support\PluginPages::pages(),
            ])
            ->resources([
                PostResource::class,
                CategoryResource::class,
                CommentResource::class,
                LinkResource::class,
                MediaResource::class,
                UserResource::class,
                ...\App\Blog\Support\PluginPages::resources(),
            ])
            ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\Filament\Widgets')
            ->widgets([
                AccountWidget::class,
            ])
            ->middleware([
                EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
                AuthenticateSession::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
            ])
            ->authMiddleware([
                Authenticate::class,
            ]);
    }
}
