<?php

namespace App\Providers;

use App\Models\Hostel;
use App\Observers\HostelObserver;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use Laravel\Dusk\DuskServiceProvider;
use Laravel\Telescope\TelescopeServiceProvider;

class AppServiceProvider extends ServiceProvider {
	/**
	 * Bootstrap any application services.
	 *
	 * @return void
	 */
	public function boot() {
		//
		//Schema::defaultStringLength(191);
        Builder::macro('toRawSql', function() {
            return array_reduce($this->getBindings(), function($sql, $binding){
                return preg_replace('/\?/', is_numeric($binding) ? $binding : "'".$binding."'" , $sql, 1);
            }, $this->toSql());
        });
		if ( request()->getHost() == 'quanlynhatro.com' ) {
			config()->set( 'services.facebook.redirect', 'https://quanlynhatro.com/connect/facebook/callback-qlnt' );
		}
		$socialite = $this->app->make('Laravel\Socialite\Contracts\Factory');
		$socialite->extend(
			'zalo',
			function ($app) use ($socialite) {
				$config = $app['config']['services.zalo'];
				return $socialite->buildProvider(ZaloProvider::class, $config);
			}
		);
	}

	/**
	 * Register any application services.
	 *
	 * @return void
	 */
	public function register() {
		//
		if ( $this->app->environment( 'local', 'testing', 'production' ) ) {
			$this->app->register( DuskServiceProvider::class );
			$this->app->register( TelescopeServiceProvider::class );
		}
	}

	protected function gate() {
		\Gate::define( 'viewTelescope', function ( $user ) {
			return in_array( $user->email, [
				'admin@itro.vn',
			] );
		} );
	}
}
