<?php

namespace App\Modules\Clients\Events;

use App\Modules\Clients\Client;
use App\Modules\Clients\Site;
use App\Modules\Clients\SiteUser;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Support\Facades\Log;
use App\Jobs\sendSiteUsertToDynamicsContact;
use Illuminate\Foundation\Bus\DispatchesJobs;


class DynamicsSiteUserSync
{
	use DispatchesJobs;
    /**
    * Subscribe to events.
    *
    * @param  mixed $events
    *
    * @return null
    */
    public function subscribe($events)
    {
        $events->listen('SiteUserUpdated', 'App\Modules\Clients\Events\DynamicsSiteUserSync@sendToDynamicsCrm');
    }

    /**
    * Creates a new client or updates an existing one
    * in DynamicsCRM with the existing details from the
    * currently updated client in Seraph.
    *
    * @param  \App\Modules\Clients\SiteUser $user
    *
    * @return array
    */
    public function sendToDynamicsCrm($user)
    {
        Log::info('DynamicsSiteUserSync sendToDynamicsCrm hit');
        Log::info('logging site user');
        Log::info($user);
        if (! config('services.dynamics.username')) {
            return;
        }
		$this->dispatch(new sendSiteUsertToDynamicsContact($user));
                return $user;
    }
}
