<?php

namespace App\Models;

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

class ProductFeature extends Model
{
    //use soft delete
    public $timestamps = false;

    protected $fillable = [
        'product_id','image'
    ];

    public static function withDescription($product_feature_id = null)
    {
        $query = self::join('product_feature_descriptions as pd', 'pd.product_feature_id', 'product_features.id')
            ->where('pd.language_id', currentLanguage()->id)
            ->select(['product_features.created_at', 'pd.*']);
        if ($product_feature_id){
            $query->where('product_features.id', $product_feature_id);
        }

        $query->select([
            'product_features.*',
            'pd.name',
            'pd.description',
        ]);
        return $query;
    }

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

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