<?php

namespace Modules\User\Providers;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Modules\User\Console\Commands\UserIdentityOver;
use Modules\User\Facades\Sms;
use Modules\User\Models\User;
use Overtrue\EasySms\EasySms;

class UserServiceProvider extends ServiceProvider
{

    /**
     * @var string $moduleName
     */
    protected string $moduleName = 'User';

    /**
     * @var string $moduleNameLower
     */
    protected string $moduleNameLower = 'user';

    protected array $runCommands = [
        UserIdentityOver::class,
    ];

    /**
     * Boot the application events.
     *
     * @return void
     */
    public function boot()
    {
        $this->loadMigrationsFrom(module_path($this->moduleName, 'Database/Migrations'));

        $this->registerValidatorExtend();

        if ($this->app->runningInConsole()) {
            $this->commands($this->runCommands);
        }
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerConfig();

        if (config('user.cache_user')) {
            Config::set('auth.providers.users.driver', 'cache');
            Config::set('cache-user', Config::get('user.cache_config'));
        }

        Config::set('auth.providers.users.model', User::class);
        Config::set('hashids.connections.code', Config::get('user.invite_code'));

        // 注册短信到容器中
        $this->app->singleton('sms', function () {
            return new EasySms(Sms::getConfig());
        });

        $this->app->register(RouteServiceProvider::class);
    }

    /**
     * Register config.
     *
     * @return void
     */
    protected function registerConfig()
    {
        $this->publishes([
            module_path($this->moduleName, 'Config/config.php')   => config_path($this->moduleNameLower.'.php'),
            module_path($this->moduleName, 'Config/identity.php') => config_path('identity.php'),
        ], 'config');

        $this->mergeConfigFrom(
            module_path($this->moduleName, 'Config/config.php'), $this->moduleNameLower
        );
        $this->mergeConfigFrom(
            module_path($this->moduleName, 'Config/identity.php'), 'identity'
        );
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides(): array
    {
        return ['sms'];
    }

    /**
     * Notes   : 注册短信验证码校验扩展
     *
     * @Date   : 2021/5/26 5:03 下午
     * @Author : <Jason.C>
     */
    public function registerValidatorExtend()
    {
        Validator::extend('sms_check', function ($attribute, $code, $parameters) {
            if (empty($code)) {
                return false;
            }
            $mobileFiled = $parameters[0] ?? 'mobile';
            $channel     = $parameters[1] ?? 'DEFAULT';
            $mobile      = request()->input($mobileFiled);

            return Sms::checkCode($mobile, $code, $channel);
        });
    }

}
