<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use SoftDeletes;
    protected $fillable = ['is_special', 'images', 'points'];

    protected $casts = [
        'images' => 'array',
        'points' => 'array',
    ];

    public function currentDescription() : HasOne
    {
        return $this->hasOne(ProductDescription::class)->where('language_id', currentLanguage()->id);
    }

    /**
     * Return descriptions for article
     * @return HasMany
     */
    public function descriptions() : HasMany
    {
        return $this->hasMany(ProductDescription::class);
    }

    public static function withDescription($ids = null)
    {
        $query = self::join('product_descriptions as pDesc', 'pDesc.product_id', 'products.id')
            ->where('pDesc.language_id', currentLanguage()->id)
            ->select(['pDesc.description', 'products.image']);

        if ($ids && is_array($ids)){
            $query->whereIn('products.id', $ids);
        }


        return $query->cursor() ?? [];
    }

}
