<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

use App\Models\Cost\{Broadband,Generic,Phone};

class Cost extends Model
{
    use HasFactory;

	protected $casts = [
		'billed_at' => 'datetime:Y-m-d',
	];

	protected $with = [
		'broadbands',
		'generics',
		'phones',
	];

	/* RELATIONS */

	public function broadbands()
	{
		return $this->hasMany(Broadband::class)
			->where('active',TRUE);
	}

	public function generics()
	{
		return $this->hasMany(Generic::class)
			->where('active',TRUE);
	}

	public function phones()
	{
		return $this->hasMany(Phone::class)
			->where('active',TRUE);
	}

	/* ATTRIBUTES */

	public function getTotalBroadbandAttribute(): float
	{
		return $this->broadbands->sum('base')+$this->broadbands->sum('excess');
	}

	public function getTotalGenericAttribute(): float
	{
		return $this->generics->sum('base')+$this->generics->sum('excess');
	}

	public function getTotalPhoneAttribute(): float
	{
		return $this->phones->sum('base')+$this->phones->sum('excess');
	}

	public function getTotalAttribute(): float
	{

		return $this->getTotalBroadbandAttribute()+$this->getTotalGenericAttribute()+$this->getTotalPhoneAttribute();
	}
}