<?php

/**
 * Service Types that have a domain name attribute.
 */
namespace App\Traits;

use App\Models\TLD;

trait ServiceDomains
{
	/* INTERFACES */

	/**
	 * There is no detailed description for domain services
	 *
	 * @return string
	 */
	public function getServiceDescriptionAttribute(): string
	{
		return '';
	}

	/**
	 * The name of the domain with its TLD
	 *
	 * @return string
	 */
	public function getServiceNameAttribute(): string
	{
		return strtoupper(sprintf('%s.%s',$this->domain_name,$this->tld->name));
	}

	/* RELATIONS */

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

	/* SCOPES */

	/**
	 * Search for a record
	 *
	 * @param        $query
	 * @param string $term
	 * @return mixed
	 */
	public function scopeSearch($query,string $term)
	{
		// If we have a period in the name, we'll ignore everything after it.
		$term = strstr($term,'.',TRUE) ?: $term;

		// Build our where clause
		return parent::scopeSearch($query,$term)
			->orwhere('domain_name','ilike','%'.$term.'%');
	}
}