<?php

namespace App;

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

class CompanyRequestTopic extends Model
{
    use SoftDeletes;

    /**
     * Return current descriptions of article
     * @return HasOne
     */
    public function currentDescription() : HasOne
    {
        return $this->hasOne(CompanyRequestTopicDescription::class)->where('language_id', currentLanguage()->id);
    }

    /**
     * Return descriptions for article
     * @return HasMany
     */
    public function descriptions() : HasMany
    {
        return $this->hasMany(CompanyRequestTopicDescription::class);
    }


    public static function withDescription($id = null)
    {
        $query = self::join('company_request_topic_descriptions as cDesc', 'cDesc.company_request_topic_id', 'company_request_topics.id')
            ->where('cDesc.language_id', currentLanguage()->id);

        if ($id){
            $query->where('company_request_topics.id', $id);
        }

        $query->select([
            'company_request_topics.id',
            'cDesc.name',
        ]);
        return $id ? $query->first() : $query->cursor() ?? [];
    }
}
