<?php

namespace Modules\Type\Models;

use App\Services\Upload\UploadService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;

class Type extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable =
    [
        // 'default_type',
        'image'
    ];

    /**
     * Get current description of the the Room
     * @return HasOne
     */
    public function currentDescription() :HasOne
    {
        return $this->hasOne(TypeDescription::class)->where('language_id', config('current_language_id'));
    }

    public function setImageAttribute($value)
    {
        if($value)
        {
            if(is_string($value))
            {
                $this->attributes['image'] = $value;
            }
            else
            {
                $oldImage= 'logo.png' != $this->image ?  $this->image : null;
                $image = UploadService::upload($value, $oldImage, 'types');
                $this->attributes['image'] = $image;
            }
        }
        else
        {
            $this->attributes['image'] = request()->method() == 'post'  ? 'logo.png' : $this->image;
        }
    }

    /**
     * Return Image URL
     * @return string|null
     */
    public function getImageUrlAttribute() : ?string
    {
        // dd(UploadService::url($this->image), $this->image);
        return UploadService::url($this->image);
    }
}
