<?php

namespace App\Models;

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

class Service extends Model
{
    use HasFactory ,SoftDeletes;
    protected $guarded = ['id', 'created_at', 'updated_at'];

    protected $appends = ['name','short_description'];

    public function getNameAttribute()
    {
        if (\app()->getLocale() == "ar") {
            return $this->name_ar;
        } else {
            return $this->name_en;
        }
    }
    public function getShortDescriptionAttribute()
    {
        if (\app()->getLocale() == "ar") {
            return $this->short_description_ar;
        } else {
            return $this->short_description_en;
        }
    }
    public function getImageAttribute($image)
    {
        if (!empty($image)) {
            return asset('uploads/service') . '/' . $image;
        }
        return "";
    }


    public function setImageAttribute($image)
    {
        if (is_file($image)) {
            $imageFields = upload($image, 'service');
            $this->attributes['image'] = $imageFields;
        }else{
            $this->attributes['image'] = $image;

        }
    }

    public function Category(){
        return $this->belongsTo(Category::class,'category_id')->withDefault([
            'name'=>'',
        ]);
    }
}
