<?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
    {
        //
    }

    private function registerMarketPurchaseListener(): void
    {
        $manager = app(\App\Blog\Support\PluginManager::class);

        if (! $manager->isEnabled('neatstudio.payment')) {
            return;
        }

        $manager->addAction('payment.paid', function (\Plugins\Neatstudio\Payment\Models\Payment $payment) {
            $payable = $payment->payable;

            if ($payable instanceof \App\Models\MarketPackage) {
                $payable->markPaid($payment);
            }
        }, 10);
    }

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

        // 商城付费：支付成功标记市场商品已购（仅当支付插件启用时注册）
        $this->registerMarketPurchaseListener();

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

        // 后台紧凑化：侧边栏 + 表格行 + 筛选区（Filament 无官方全局紧凑开关，用 CSS 收紧）
        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; }

    /* 表格行紧凑：数据行高度由 .fi-ta-text / .fi-ta-icon 的 padding-block 驱动；操作列按钮禁止换行堆叠 */
    .fi-ta-text, .fi-ta-icon { padding-block: calc(var(--spacing) * 1.75) !important; }
    .fi-ta-actions { flex-wrap: nowrap !important; min-height: 0 !important; }
    .fi-ta-header-cell { padding-block: calc(var(--spacing) * 4) !important; }
    .fi-ta-selection-cell { padding-block: calc(var(--spacing) * 1.5) !important; }
    .fi-ta-header-toolbar { padding-block: 0.5rem !important; }

    /* 标题列过长单行省略，避免换行撑高行 */
    .fi-ta-cell-title .fi-ta-text { display: block !important; white-space: nowrap !important; overflow: hidden !important; text-overflow: ellipsis !important; }
    .fi-ta-cell-title .fi-ta-text .fi-ta-text-item,
    .fi-ta-cell-title .fi-ta-text .fi-ta-text-description { display: inline !important; margin: 0 !important; }

    /* 操作列固定宽度（钉住右侧需 table sticky，见备注） */
    .fi-ta-actions-header-cell, .fi-ta-cell:has(.fi-ta-actions) { width: 88px !important; min-width: 88px !important; }

    /* 文章编辑框加大（MarkdownEditor 基于 tiptap） */
    .fi-fo-markdown-editor .tiptap, .fi-fo-markdown-editor .ProseMirror { min-height: 26rem !important; }

    /* 筛选条件区：整体一行横排，重置紧跟应用按钮，控件紧凑 */
    .fi-ta-filters-above-content-ctn { padding: 0.4rem 0.9rem !important; }
    .fi-ta-filters-heading { display: none !important; }
    .fi-ta-filters { display: flex !important; flex-wrap: wrap; align-items: center; gap: 0.5rem 0.9rem; }
    .fi-ta-filters-header { margin: 0 !important; order: 99 !important; }
    .fi-ta-filters .fi-grid { display: flex !important; flex-wrap: wrap; align-items: center; gap: 0.5rem 0.9rem; }
    .fi-ta-filters .fi-fo-field { display: flex !important; align-items: center; gap: 0.4rem; }
    .fi-ta-filters .fi-fo-field-label { margin-bottom: 0 !important; }
    .fi-ta-filters .fi-input-wrp { min-height: 1.5rem !important; border-radius: 0.4rem !important; padding-block: 0 !important; }
    .fi-ta-filters .fi-select-input, .fi-ta-filters .fi-select-native { font-size: 0.8rem !important; min-height: 0 !important; height: auto !important; padding-block: 0.15rem !important; }
    .fi-ta-filters-actions-ctn { display: inline-flex !important; }
    .fi-ta-filters-actions-ctn .fi-btn { padding: 0.2rem 0.7rem !important; font-size: 0.8rem !important; }
</style>
HTML)
            );
        });
    }
}
