<?php

namespace App\Providers;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Laravel\Passport\Passport;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Passport::hashClientSecrets();
        // Passport::tokensCan(['openid' => 'OpenID Connect']);
        Schema::defaultStringLength(191);
        JsonResource::withoutWrapping();
        Validator::extend('htmlMax', function ($attribute, $htmlValue, $parameters, $validator) {
            $validator->requireParameterCount(1, $parameters, 'htmlMax');
            $value = strip_tags($htmlValue);
            return $validator->validateMax($attribute, $value, $parameters);
        });

        Validator::replacer('htmlMax', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':max', $parameters[0], $message);
        });

        Validator::extend('strong_password', function ($attribute, $value, $parameters, $validator) {
            return preg_match('/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[^\w\d\s])[\w\W]{8,24}$/', $value);
        });

        Validator::replacer('strong_password', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':attribute', $attribute, 'The :attribute must be contain at least 1 uppercase, 1 lowercase, numeric character and special character.');
        });

        if($this->app->environment('production')) {
            URL::forceScheme('https');
        }
    }
}
