<?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 ConsultingTopic extends Model
{
    use SoftDeletes;

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

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

    public static function withDescription($topicId = null)
    {
        $query = self::join('consulting_topic_descriptions as dtDesc', 'dtDesc.consulting_topic_id', 'consulting_topics.id')
            ->where('dtDesc.language_id', currentLanguage()->id);

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

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