<?php

namespace Modules\Product\Models;

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

class ProductItem extends Model
{
    public $timestamps = false;

    protected $fillable =
        [
            'product_id',
        ];

    public function items()
    {
        return $this->hasMany(Item::class,'product_item_id');
    }

    public function descriptions($language_id= null)
    {
        if ($language_id) {
            $data = $this->hasMany(ProductItemDescription::class)->where('language_id', $language_id)->first();
            if(!$data){
                $data = $this->hasMany(ProductItemDescription::class)->first();
            }
            return $data;
        }
        $data = $this->hasMany(ProductItemDescription::class)->where('language_id', currentLanguage()['id'])->first();
        return $data;
    }


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