<?php

namespace App\Models;

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

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

    protected $appends = ['name','shot_description','long_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 getLongDescriptionAttribute()
    {
        if (\app()->getLocale() == "ar") {
            return $this->long_description_ar;
        } else {
            return $this->long_description_en;
        }
    }
    public function getImageAttribute($image)
    {
        if (!empty($image)) {
            return asset('uploads/event') . '/' . $image;
        }
        return "";
    }


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

        }
    }

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

    public function subscribers()
    {
        return $this->hasMany(CourseSubscribe::class,'course_id');
    }
}
