<?php

namespace Kylin\Settings;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Kylin\Settings\Models\Setting;
use Kylin\Settings\Policies\SettingPolicy;
use Kylin\Settings\Services\SettingsService;

/**
 * 系统设置服务提供者
 *
 * 提供键值对形式的系统配置管理，支持：
 *  - 分组管理（Group）
 *  - 类型化配置（string/int/bool/json/encrypted）
 *  - 加密存储（encrypted 类型自动加密/解密）
 *  - 缓存加速读取
 *  - 审计日志
 *  - 权限策略
 *
 * 合规要求：
 *  - 依据等保 2.0 (GB/T 22239) 8.1.4.1，注册 SettingPolicy 权限策略
 *  - 依据《密码法》第二十七条，密钥/Token 类配置加密存储
 */
class SettingsServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->mergeConfigFrom(__DIR__ . '/../config/kylin-settings.php', 'kylin-settings');
        $this->app->singleton(SettingsService::class);
        $this->app->singleton('kylin-settings', SettingsService::class);
    }

    public function boot(): void
    {
        $this->publishes([__DIR__ . '/../config/kylin-settings.php' => config_path('kylin-settings.php')], 'kylin-settings-config');
        $this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
        $this->loadRoutesFrom(__DIR__ . '/../routes/api.php');

        // 注册权限策略
        Gate::policy(Setting::class, SettingPolicy::class);
    }
}