<?php

namespace App\Models;

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

/**
 * @property mixed id
 */
class Rate extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable = [
        'image','rate','name','comment','user_id','product_id','status'
    ];

    public function user()
    {
        if($this->user_status == 'visitor')
        {
            return $this->belongsTo(Visitor::class,'user_id')->withTrashed();
        }
        return $this->belongsTo(User::class,'user_id')->withTrashed();
    }

    public function product()
    {
        return $this->belongsTo(Product::class,'product_id')->withTrashed();
    }

    public function getDateAttribute()
    {
        $date = date('Y-m-d',strtotime($this->created_at));
        return $date;
    }
    public function getUserImageAttribute()
    {
        $image = $this->image;
        if($this->user_id)
        {
            $image = $this->user ? $this->user->photo : 'user.png';
        }
        return asset($image);
    }
    public function getUserNameAttribute()
    {
        $name = $this->name;
        if($this->user_id)
        {
            $name = $this->user ? $this->user->name : '';
        }
        return $name;
    }
}
