<?php

namespace App\Models;

use App\Models\User;
use App\Models\Product;
use Illuminate\Database\Eloquent\Model;

/**
 * @package App\Models
 * @property int|null $user_id
 * @property string|null $token
 * @property int $product_id
 *
 * @property-read User $user
 * @property-read Product $product
 */
class Favorite extends Model
{
    protected $table = 'products_users';

    public $timestamps = false;

    public $incrementing = false;

    /**
     * @var array
     */
    protected $fillable = [
        'user_id', 'product_id', 'token'
    ];

    /**
     * @var array
     */
    protected $casts = [
        'user_id' => 'integer',
        'product_id' => 'integer',
        'token' => 'string'
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    /**
     * @param $query
     * @param $token
     * @return mixed
     */
    public function scopeFindByToken($query, $token)
    {
        return $query->where('token', $token);
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeFindByList($query)
    {
        if (auth()->check()) {
            return $query->where('user_id', auth()->user()->id);
        }

        return $query->where('token', request()->cookie('token'));
    }
}
