<?php

namespace App\Models\Product;

use Illuminate\Database\Eloquent\Model;

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

/**
 * Class Supplier\Type
 *
 * Attributes for supplier types:
 * + billing_interval		: The Billing interval that the supplier normally uses for this offering
 */

abstract class Type extends Model implements ProductItem
{
	use SiteID,OrderServiceOptions;

	public function __get($key): mixed
	{
		return match ($key) {
			'billing_interval' => static::DefaultBill,

			default => parent::__get($key),
		};
	}

	/* 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 product contract term is the highest of our defined contract_term (in Products/*) vs the suppliers
	 * contract term (defined in Supplier/*).
	 *
	 * @param int|null $val
	 * @return int
	 */
	public function getContractTermAttribute(?int $val): int
	{
		return max($val ?: 0,$this->supplied->contract_term);
	}
}