<?php

declare(strict_types=1);

namespace Kylin\RiskEngine;

use Illuminate\Support\ServiceProvider;
use Kylin\RiskEngine\Services\FaceVerificationService;
use Kylin\RiskEngine\Services\RiskEngineService;

/**
 * 麒麟游戏 风控引擎服务提供者
 *
 * 负责注册：
 * - 配置合并
 * - 数据库迁移
 * - 风控核心服务（评分卡 + 黑白名单 + 决策路由）
 * - 人脸复核服务
 * - HTTP 路由
 *
 * 法律合规：
 *  - 《未成年人保护法》第七十六条：建立未成年人保护风控机制
 *  - 《个人信息保护法》第二十八条：人脸信息为敏感个人信息
 *  - 等保 2.0 8.1.4.6：风控决策审计日志
 */
final class RiskEngineServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->mergeConfigFrom(
            __DIR__ . '/../config/kylin-risk-engine.php',
            'kylin-risk-engine'
        );

        $this->app->singleton(RiskEngineService::class, function ($app) {
            return new RiskEngineService(
                config('kylin-risk-engine.rules', []),
                $app['cache']->store(config('kylin-risk-engine.cache_store', 'redis'))
            );
        });

        $this->app->singleton(FaceVerificationService::class, function ($app) {
            return new FaceVerificationService(
                $app['cache']->store(config('kylin-risk-engine.cache_store', 'redis'))
            );
        });
    }

    public function boot(): void
    {
        if ($this->app->runningInConsole()) {
            $this->publishes([
                __DIR__ . '/../config/kylin-risk-engine.php' => config_path('kylin-risk-engine.php'),
            ], 'kylin-risk-engine-config');

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

            $this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
        }

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