<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;

class Branch extends Model
{
    // Fields that mass assignment
    protected $fillable = [ 'meta'];

    // Use soft delete
    use SoftDeletes;

    /**
     * descrips how the model cast the field value
     */
    protected $casts = ['meta' => 'array'];
    
    /**
     * Return Persons Emails
     * @return array
     */
    public function getPersonsEmailsAttribute(): array
    {
        return $this->meta ? $this->meta['persons_emails'] ?? []: [];
    }
    
    /**
     * Return Persons Phones
     * @return array
     */
    public function getPersonsPhonesAttribute(): array
    {
        return $this->meta ? $this->meta['persons_phones'] ?? []: [];
    }

    /**
     * Get the Latitude
     *
     * @return string
     */
    public function getLatitudeAttribute(): ?string
    {
        return $this->meta ? $this->meta['latitude'] ?? null : null;
    }
    
    /**
     * Get the Longitude
     *
     * @return string
     */
    public function getLongitudeAttribute(): ?string
    {
        return $this->meta ? $this->meta['longitude'] ?? null : null;
    }
    
    /**
     * Get the Phone
     *
     * @return string
     */
    public function getPhoneAttribute(): ?string
    {
        return $this->meta ? $this->meta['phone'] ?? null : null;
    }
    
    
    /**
     * Get the Email
     *
     * @return string
     */
    public function getEmailAttribute(): ?string
    {
        return $this->meta ? $this->meta['email'] ?? null : null;
    }

    /**
     * Return current descriptions of article
     * @return HasOne
     */
    public function currentDescription() : HasOne
    {
        return $this->hasOne(BranchDescription::class)->where('language_id', currentLanguage()->id);
    }

    /**
     * Get description for pages
     * @return HasMany
     */
    public function descriptions() :HasMany
    {
        return $this->hasMany(BranchDescription::class);
    }
}
