<?php

declare(strict_types=1);

namespace App\Providers;

use Filament\Facades\Filament;
use Filament\View\PanelsRenderHook;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        // 自动加载所有插件的迁移（插件目录下 database/migrations）
        foreach (glob(base_path('plugins/*/database/migrations')) ?: [] as $path) {
            $this->loadMigrationsFrom($path);
        }

        // 前台页面缓存（匿名 GET，缓存含 SEO 注入后的最终 HTML）
        app('router')->pushMiddlewareToGroup('web', \App\Blog\Support\PageCacheMiddleware::class);

        // 后台侧边栏紧凑化（更窄的菜单项）
        Filament::serving(function () {
            Filament::registerRenderHook(
                PanelsRenderHook::STYLES_AFTER,
                fn () => Blade::render(<<<'HTML'
<style>
    .fi-sidebar { padding-inline: 0.75rem; }
    .fi-sidebar-nav { padding-inline: 0; }
    .fi-sidebar-nav-groups { margin-inline: 0; }
    .fi-sidebar-nav .fi-sidebar-item-button { padding-inline: 0.6rem; border-radius: 0.5rem; }
    .fi-sidebar-nav .fi-sidebar-item-label { font-size: 0.82rem; }
    .fi-sidebar-nav .fi-sidebar-group-label { font-size: 0.68rem; letter-spacing: 0.04em; }
    .fi-sidebar-brand { padding-block: 0.9rem; }
</style>
HTML)
            );
        });
    }
}
