<?php

namespace App;

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

/**
 * Class Service
 * @package App
 * @mixin Builder
 */
class Occasion extends Model
{
    use SoftDeletes;

    protected $fillable = ['image', 'url'];


    public function currentDescription() : HasOne
    {
        return $this->hasOne(OccasionDescription::class)->where('language_id', currentLanguage()->id);
    }

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

    public static function withDescription($ids = null)
    {
        $query = self::join('occasion_descriptions as oDesc', 'oDesc.occasion_id', 'occasions.id')
            ->where('oDesc.language_id', currentLanguage()->id)
            ->select(['oDesc.description', 'occasions.image']);

        if ($ids && is_array($ids)){
            $query->whereIn('occasions.id', $ids);
        }


        return $query->cursor() ?? [];
    }

}
