<?php

declare(strict_types=1);

namespace Plugins\Neatstudio\LinkTracker;

use App\Blog\Support\PluginManager;
use App\Blog\Support\PluginServiceProvider;
use Plugins\Neatstudio\LinkTracker\Services\UrlRedirector;

class ServiceProvider extends PluginServiceProvider
{
    protected function boot(PluginManager $manager): void
    {
        $this->loadRoutes(__DIR__.'/../routes/web.php');

        // 核心视图（评论正文/作者网址/友链）通过 tracked_url() 走此过滤器改写
        $manager->addFilter('link.redirect', function (string $url) {
            return app(UrlRedirector::class)->goUrl($url);
        }, 10);

        // 正文外链改写：优先级 20，晚于会员付费内容过滤（10），保证 paywall 也覆盖
        $manager->addFilter('post.rendered', function (string $html, \App\Models\Post $post) {
            return app(UrlRedirector::class)->rewriteExternalLinks($html);
        }, 20);
    }
}
