<?php

namespace App\Microsoft\Dynamics;

use App\Modules\Clients\Client;
use App\Modules\Clients\Site;
use App\Modules\Clients\SiteUser;
use Illuminate\Support\Facades\Log;

class Account
{
	/**
	* All properties used by default.
	*
	* @var array
	*/
	protected $attributes = [
		'name'                => null,
		'accountid'           => null,
		'websiteurl'          => null,
		'new_seraph_id'       => null,
		'address1_line1'      => null,
		'address1_city'       => null,
		'address1_postalcode' => null,
		'address1_telephone1' => null,
		'telephone1'          => null,
		'emailaddress1'       => null,
	];

	/**
	* Create a new instance of the Account entity.
	*
	* @param array $attributes
	*/
	public function __construct($attributes = [])
	{
		$this->attributes = array_merge($this->attributes, $attributes);
	}

	/**
	* Creates a new instance of a client.
	*
	* @return \App\Modules\Clients\Client
	*/
	public function toClient()
	{
		Log::info("toClient hit");
		Log::info("logging all attributes");
		Log::info($this->attributes);

		if(isset($this->attributes['accountid']) && isset($this->attributes['new_seraph_id'])) {
			Log::info("dynamics id and seraph id set");
			Log::info("Account toClient | looging all attributes");
			Log::info($this->attributes);
			$client = Client::where('dynamics_id', $this->attributes['accountid'])
			->orWhere('id', $this->attributes['new_seraph_id'])
			->first();
		}
		else if(isset($this->attributes['new_seraph_id'])) {
			Log::info("only seraph id set");
			Log::info("Account toClient | looging all attributes");
			Log::info($this->attributes);
			$client = Client::where('id', $this->attributes['new_seraph_id'])
			->first();
		}
		else if (isset($this->attributes['accountid'])) {
			Log::info("Account toClient | looging all attributes");
			Log::info("only dynamics id set");
			Log::info($this->attributes);
			$client = Client::where('dynamics_id', $this->attributes['accountid'])
			->first();
		}

		if (! $client) {
			$client = new Client;
		}
		else {

			//     client variables
			// protected $fillable = [
			// 'name', 'primary_site', 'primary_contact', 'contract_id', 'contract_price', 'contract_start_date', 'contract_end_date', 'contract_monthly_allowance',
			// 'sla_id', 'website', 'followup_exempt', 'blind_notes', 'flagged', 'dynamics_id',
			//]

			// site variables
			// protected $fillable = ['name', 'client_id', 'address', 'address2', 'postcode', 'primary_phone'];
			// get site associated with client

			$site = Site::where('client_id', $client->id)->first();
			Log::info("logging clientSite");
			Log::info($site);

			if($site) {
				$site->fill([
					'address'  			=> ($this->attributes['address1_line1'] == null) ? $site->address : $this->attributes['address1_line1'],
					'address2'  		=> ($this->attributes['address1_city'] == null) ? $site->address2 : $this->attributes['address1_city'],
					'postcode'  		=> ($this->attributes['address1_postalcode'] == null) ? $site->postcode : $this->attributes['address1_postalcode'],
					'primary_phone'  	=> ($this->attributes['telephone1'] == null) ? $site->primary_phone : $this->attributes['telephone1'],
				]);
				$site->save();
			}


			$siteUser = SiteUser::where('site_id', $site->id)->first();

			if ($siteUser) {
				Log::info("Account | siteUser found");
				Log::info("logging siteuser before filling the siteUser");
				Log::info($siteUser);

				$siteUser->fill([
					'email' => ($this->attributes['emailaddress1'] == null) ? $siteUser->email : $this->attributes['emailaddress1'],
				]);
				$siteUser->save();
			}
		}

		$client->fill([
			'name'  		=> ($this->attributes['name'] == null) ? $client->name : $this->attributes['name'],
			'dynamics_id'  	=> ($this->attributes['accountid'] == null) ? $client->dynamics_id : $this->attributes['accountid'],
			'website'   	=> ($this->attributes['websiteurl'] == null) ? $client->website : $this->attributes['websiteurl'],
		]);

		return $client;
	}
}
