<?php

namespace App\Providers;

use App\AdminAccessRight;
use App\Company;
use App\CompanyEmail;
use App\CompanyStatus;
use App\Http\View\Composers\LogComposer;
use App\LogReceived;
use App\MediaPrice;
use App\Traits\ChimpTrait;
use App\User;
use App\UserPost;
use App\UserSenderId;
use App\UserSocial;
use Carbon\Carbon;
use DrewM\MailChimp\MailChimp;
use Facebook\Exceptions\FacebookSDKException;
use Facebook\Facebook;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;
use Mandrill;
use PhpParser\Node\Expr\ShellExec;
use function Sodium\add;

class AppServiceProvider extends ServiceProvider
{





    public  function init()
    {
        return new MailChimp(env('MAIL_CHIMP'));
    }

    public function boot()
    {

        Schema::defaultStringLength(191);



//        dd(round('255.4', 3, PHP_ROUND_HALF_DOWN));


//        $password = 'password';
//        $options = [
//            'cost' => 10,
//        ];
//        $hash =  password_hash($password, PASSWORD_BCRYPT, $options);
//
//        if (password_verify($password, $hash))
//        {
//            dd('yes');
//        }else{
//            dd('failed');
//        }

//
//        $chimp = $this->init();
//        $camps = $chimp->get("/reports/e597e117c8");
//
//        dd($camps);

            // Add logs to when user routes to all views
            view()->composer('*', LogComposer::class);

            view()->composer('*', function ($view) {

            $is_reseller_client = false;

            $user = Auth::user();

            if(!empty($user)) {
                if($user->created_by!=='npontu') {
                    $is_reseller_client = true;
                }
            }

            $logo = asset('img/logo.png');

            $price = MediaPrice::query()->where('is_active',true)->select('cost')->first();

            if(Auth::user() && !empty(Auth::user()->admin_access_rights->id))
            {
                $id = Auth::user()->admin_access_rights->value;

                if($id == 1)
                       $company = CompanyStatus::query()->whereHas('company')->with('company')
                    ->where('company_statuses.status',3)->get()->count();
                elseif ($id == 3)
                    $company = CompanyStatus::query()->whereHas('company')->with('company')
                        ->where('company_statuses.status',3)->get()->count();
                elseif($id == 2)
                    $company = CompanyStatus::query()->whereHas('company')->with('company')
                        ->where('company_statuses.status',3)->get()->count();
                else
                    $company = CompanyStatus::query()->whereHas('company')->with('company')->get()->count();

            }

            $company_approved = CompanyStatus::query()
                ->whereHas('company')->with('company')
                ->where('company_statuses.status',1)
                ->get()->count();

            $sender_id_count = UserSenderId::query()->whereHas('user')->where('status',false)->get()->count();


            $view->with([
                'ulogo' =>$logo,
                '_user'  => $user,
                'is_reseller_client' => $is_reseller_client,
                'company_pending'    => @$company,
                'company_approved'   => @$company_approved,
                'sender_id_count'    => $sender_id_count,
                'company_email'      => null,
                'email_sources'      => null,
                'price'              => @$price->cost
            ]);
        });

    }


    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('mailgun.client', function() {
            return \Http\Adapter\Guzzle6\Client::createWithConfig([
                // your Guzzle6 configuration
                'verify' => false
            ]);
        });
    }



}
