<?php

namespace App\Models;

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

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

    protected $fillable = [
        'image',
    ];


    public static function withDescription($review_id = null)
    {
        $query = self::orderByDesc('reviews.id')->join('review_descriptions as sd', 'sd.review_id', 'reviews.id')
            ->where('sd.language_id', currentLanguage()->id)
            ->select(['reviews.created_at', 'sd.*']);
        if ($review_id){
            $query->where('reviews.id', $review_id);
        }

        $query->select([
            'reviews.*',
            'sd.name',
            'sd.description',
            'sd.user',
        ]);
        return $query;
    }
    public function descriptions($language_id= null)
    {
        if ($language_id) {
            $data = $this->hasMany(ReviewDescription::class)->where('language_id', $language_id)->first();
            if(!$data){
                $data = $this->hasMany(ReviewDescription::class)->first();
            }
            return $data;
        }
        $data = $this->hasMany(ReviewDescription::class)->where('language_id', currentLanguage()['id'])->first();
        return $data;
    }
}
