<?php

namespace Modules\Product\Models;

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

class Item extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable =
        [
            'product_item_id','cost'
        ];

    public function productItem()
    {
        return $this->belongsTo(ProductItem::class,'product_item_id')->withTrashed();
    }

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


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