<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Service extends Model
{
	protected $table = 'ab_service';
	protected $dates = ['date_last_invoice','date_next_invoice'];
	protected $with = ['product.descriptions','account.language','service_adsl','service_domain.tld','service_ssl'];

	public function account()
	{
		return $this->belongsTo(\App\User::class);
	}

	public function service_adsl()
	{
		return $this->belongsTo(ServiceAdsl::class,'id','service_id');
	}

	public function service_domain()
	{
		return $this->belongsTo(ServiceDomain::class,'id','service_id');
	}

	public function service_ssl()
	{
		return $this->belongsTo(ServiceSsl::class,'id','service_id');
	}

	public function product()
	{
		return $this->belongsTo(Product::class);
	}

	public function getCategoryAttribute()
	{
		return $this->product->prod_plugin_file;
	}

	public function getNextInvoiceAttribute()
	{
		return $this->date_next_invoice->format('Y-m-d');
	}

	/**
	 * This function will return the associated service model for the product type
	 */
	public function getServiceDetail()
	{
		switch ($this->product->prod_plugin_file)
		{
			case 'ADSL': return $this->service_adsl;
			case 'DOMAIN': return $this->service_domain;
			case 'SSL': return $this->service_ssl;
			default: abort(500,'Havent handled case for: '.$this->product->prod_plugin_file);
		}

	}

	public function getServiceExpireAttribute()
	{
		return 'TBA';
	}

	public function getServiceNameAttribute()
	{
		if (! isset($this->getServiceDetail()->name)) dd($this,$this->product,$this->getServiceDetail());
		return sprintf('%s: %s',$this->product->name($this->account->language),$this->getServiceDetail()->name);
	}

	public function getServiceNumberAttribute()
	{
		return sprintf('%02s.%04s.%04s',$this->site_id,$this->account_id,$this->id);
	}
}