<?php

namespace App\Models\Supplier;

use Illuminate\Support\Collection;

use App\Models\Product\Domain as ProductDomain;
use App\Models\TLD;

final class Domain extends Type
{
	protected const category_name = 'Domain';

	protected $table = 'supplier_domain';

	// The model of the product that is supplied by this model
	const ProductModel = ProductDomain::class;

	public function __get($key): mixed
	{
		return match ($key) {
			'name' => sprintf('%s: %s',$this->product_id,$this->tld->name),

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

	/* STATIC */

	/**
	 * Determine the owner of the name servers used for the domain
	 *
	 */
	public static function nameserver_name(Collection $nameservers): string
	{
		foreach (config('nameservers') as $key => $ns) {
			if ($nameservers->diff($ns)->count() < count($ns))
				return $key;
		}

		return 'custom';
	}

	/* RELATIONS */

	public function tld()
	{
		return $this->belongsTo(TLD::class);
	}
}