<?php

namespace Kylin\User;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Kylin\User\Commands\CleanUserDataCommand;
use Kylin\User\Models\User;
use Kylin\User\Policies\UserPolicy;
use Kylin\User\Services\UserService;

/**
 * 用户服务提供者
 *
 * 提供用户管理核心功能，包括用户注册、信息维护、
 * 实名认证、用户查询、数据清理、权限策略。
 *
 * 合规要求：
 *  - 依据等保 2.0 (GB/T 22239) 8.1.4.1，注册 UserPolicy 权限策略
 *  - 依据《个人信息保护法》第四十七条，提供数据清理命令
 */
class UserServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->mergeConfigFrom(
            __DIR__ . '/../config/kylin-user.php', 'kylin-user'
        );

        $this->app->singleton(UserService::class);
        $this->app->singleton('kylin-user', UserService::class);
    }

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

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

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

        // 注册权限策略
        Gate::policy(User::class, UserPolicy::class);

        // 注册控制台命令
        if ($this->app->runningInConsole()) {
            $this->commands([
                CleanUserDataCommand::class,
            ]);
        }
    }
}