<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Leenooks\Traits\ScopeActive;

use App\Models\Supplier\{Broadband,Domain,Ethernet,Generic,Host,HSPA,Voip};

class Supplier extends Model
{
	use ScopeActive;

	public $timestamps = FALSE;

	/* The offerings we provide */
	public const offering_types = [
		'broadband' => [
			'name' => 'Broadband',
			'class' => Broadband::class,
		],
		'hspa' => [
			'name' => 'Mobile Broadband',
			'class' => HSPA::class,
		],
		'ethernet' => [
			'name' => 'Ethernet Broadband',
			'class' => Ethernet::class,
		],
		'domainname' => [
			'name' => 'Domain Name',
			'class' => Domain::class,
		],
		'generic' => [
			'name' => 'Generic',
			'class' => Generic::class,
		],
		'hosting' => [
			'name' => 'Hosting',
			'class' => Host::class,
		],
		'voip' => [
			'name' => 'VOIP Telephone',
			'class' => Voip::class,
		],
	];

	/* RELATIONS */

	public function detail()
	{
		return $this->hasOne(SupplierDetail::class);
	}

	/* METHODS */

	/**
	 * Return the offerings that this supplier provides
	 *
	 * @return void
	 */
	public function offeringTypes(): Collection
	{
		$result = collect();

		// See if we have any configurations
		foreach (self::offering_types as $key => $type) {
			if (! ($class=Arr::get($type,'class')))
				continue;

			if (Arr::get($this->detail->connections,$key)) {
				$result->put($key,(object)[
					'type' => Arr::get($type,'name'),
					'items' => (new $class)->where('supplier_detail_id',$this->detail->id),
					]);

				continue;
			}

			// See if we have any products defined
			$o = new $class;
			$o->where('supplier_detail_id',$this->detail->id);

			if ($o->count())
				$result->put($key,(object)[
					'type' => Arr::get($type,'name'),
					'items' => (new $class)->where('supplier_detail_id',$this->detail->id),
				]);
		}

		return $result;
	}
}