<?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, $skip = null)
    {
        $query = self::join('occasion_descriptions as oDesc', 'oDesc.occasion_id', 'occasions.id')
            ->where('oDesc.language_id', currentLanguage()->id)
            ->limit(6)
            ->select(['oDesc.name', 'occasions.image', 'occasions.url']);

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

        if ($skip){
            $query->skip($skip)->take(6);
        }


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

}
