<?php

namespace Kylin\WordFilter;

use Illuminate\Support\ServiceProvider;
use Kylin\WordFilter\Console\ImportSensitiveWordsCommand;
use Kylin\WordFilter\Services\VariantDetector;
use Kylin\WordFilter\Services\WordFilterService;
use Kylin\WordFilter\Services\WordSyncService;

/**
 * 敏感词过滤服务提供者
 *
 * 注册敏感词过滤核心服务，包括：
 *  - VariantDetector：变体检测（谐音、拆字、拼音等）
 *  - WordFilterService：基于 Trie + DFA 的高效过滤引擎
 *  - WordSyncService：词库版本管理与同步
 *
 * 路由注册、命令注册、配置发布。
 */
class WordFilterServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->mergeConfigFrom(
            __DIR__ . '/../config/kylin-word-filter.php', 'kylin-word-filter'
        );

        // 注册变体检测服务
        $this->app->singleton(VariantDetector::class);

        // 注册过滤核心服务
        $this->app->singleton(WordFilterService::class);
        $this->app->singleton('kylin-word-filter', WordFilterService::class);

        // 注册同步服务
        $this->app->singleton(WordSyncService::class);
    }

    public function boot(): void
    {
        $this->loadMigrationsFrom(__DIR__ . '/../database/migrations');

        $this->publishes([
            __DIR__ . '/../database/migrations' => database_path('migrations'),
        ], 'kylin-word-filter-migrations');

        $this->publishes([
            __DIR__ . '/../config/kylin-word-filter.php' => config_path('kylin-word-filter.php'),
        ], 'kylin-word-filter-config');

        $this->loadRoutesFrom(__DIR__ . '/../routes/api.php');

        // 注册 Artisan 命令
        if ($this->app->runningInConsole()) {
            $this->commands([
                ImportSensitiveWordsCommand::class,
            ]);
        }
    }
}
