<?php

namespace App\Models\Product;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;

use App\Models\Product;
use App\Traits\{OrderServiceOptions,ProductDetails,SiteID};

abstract class Type extends Model
{
	use SiteID,ProductDetails,OrderServiceOptions;

	/* RELATIONS */

	/**
	 * The product that sells this type
	 *
	 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
	 */
	final public function products()
	{
		return $this->morphMany(Product::class, null,'model','model_id');
	}

	/**
	 * The offering supplied with this product
	 *
	 * @return \Illuminate\Database\Eloquent\Relations\HasOne
	 */
	final public function supplied()
	{
		return $this->hasOne(static::SupplierModel,'id','supplier_item_id');
	}

	/* ATTRIBUTES */

	/**
	 * The Billing interval that the supplier normally uses for this offering
	 *
	 * @return int
	 */
	public function getBillingIntervalAttribute(): int
	{
		return static::DefaultBill;
	}

	/**
	 * The product contract term is the highest of our defined contract_term (in Products/*) vs the suppliers
	 * contract term (defined in Supplier/*).
	 *
	 * @return int
	 */
	public function getContractTermAttribute(): int
	{
		return max(Arr::get($this->attributes,'contract_term',0),$this->supplied->contract_term);
	}
}