<?php

namespace App\Models;

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

class Rating extends Model
{
    use HasFactory;
    protected $fillable = [ 'user_id', 'store_detail_id', 'rating', 'review','order_id' ];
    protected $casts = [
        'user_id'    => 'integer',
        'rating'     => 'double',
        'product_id' => 'integer',
        'order_id'   => 'integer',
    ];
    public function scopeProductRating($query)
    {
        $user = auth()->user();

        if($user->user_type == 'storeowner') {
            return $query->whereHas('product', function ($q) use($user) {
                $q->where('storeowner_id', $user->id);
            });
        }
        if($user->user_type == 'client') {
            return $query->where('user_id', $user->id);
        }
        return $query;
    }
    public function store() {
        return $this->belongsTo(Store::class, 'store_detail_id', 'id');
    }
    public function user() {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
}
