<?php

namespace App\Models;

use Cviebrock\EloquentSluggable\Sluggable;
use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Service extends Model
{
    use HasFactory;
    use Sluggable;
    use SluggableScopeHelpers;

    protected $fillable = [
        'name_ar',
        'name_en',
        'desc_ar',
        'desc_en',
        'photo',
        'parent_id',
        'slug',
    ];

    // get name translation
    public function getNameAttribute()
    {
        return $this->attributes['name_' . app()->getLocale()];
    } // end getNameAttribute

    // get desc translation
    public function getDescAttribute()
    {
        return $this->attributes['desc_' . app()->getLocale()];
    } // end getDescAttribute


    public function keywords()
    {
        return $this->morphMany(KeyWord::class, 'keywordable');
    }

    public function imagewithdesc()
    {
        return $this->hasMany(ImageWithDescService::class, 'service_id', 'id');
    }

    public function childs()
    {
        return $this->hasMany(Service::class, 'parent_id', 'id');
    }

    public function parent()
    {
        return $this->belongsTo(Service::class, 'parent_id', 'id');
    }

    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'name_en',
                'onUpdate' => false,
            ]
        ];
    }
}
