<?php

namespace App\Models;

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

/**
 * @property mixed id
 */
class Choise extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable = [
        'status'
    ];

    /**
     * Return descriptions for choise
     * @return HasMany
     */
    public function descriptions($language_id= null)
    {
        if ($language_id) {
            $data = $this->hasMany(ChoiseDescription::class)->where('language_id', $language_id)->first();
            if(!$data){
                $data = $this->hasMany(ChoiseDescription::class)->first();
            }
            return $data;
        }
        $data = $this->hasMany(ChoiseDescription::class)->where('language_id', currentLanguage()['id'])->first();
        return $data;
    }
    public static function withDescription($choise_id = null)
    {
        $query = self::orderByDesc('choises.id')->join('choise_descriptions as ad', 'ad.choise_id', 'choises.id')
            ->where('ad.language_id', currentLanguage()->id)
            ->select(['choises.created_at', 'ad.*']);

        if ($choise_id)
        {
            if (is_array($choise_id))
            {
                $query->whereIn('choises.id', $choise_id);
            }
            else
            {
                $query->where('choises.id', $choise_id);
            }
        }
        $query->select([
            'choises.id',
            'choises.status',
            'ad.name',
        ]);
        return $query;
    }

}
