<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class Faq extends Model
{
    // For Trash
    use SoftDeletes;
    protected $guarded = [];

    /**
     * Returns Faq Descriptions
     *
     * @return HasMany
    */
    public function descriptions(): HasMany
	{
        return $this->hasMany(FaqDescription::class);
    }

    /**
     * Returns Faq Lesson
     *
     * @return belongsTo
    */
    public function lesson(): BelongsTo
    {
        return $this->belongsTo(Lesson::class);
    }

    /**
     * Returns All Categories With Their Descriptions
     *
     * @return Query
    */
    public static function GetFaqWithDescriptions($type)
    {
        $query = self::latest()
        ->join('faq_descriptions AS fd', 'faqs.id', 'fd.faq_id')
        ->leftJoin('lesson_descriptions AS ld', function ($join) {
            $join->on('ld.lesson_id', 'faqs.lesson_id')
                 ->where('ld.language_id', currentLanguage()->id);
        })
        ->join('languages', 'languages.id', 'fd.language_id')
        ->where('local', getCurrentLocale())
        ->select(['fd.question', 'faqs.*' , 'ld.name']);

        return $type == 'get' ? $query->cursor() : $query;
    }

}
