<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Installment extends Model
{
    //
    use SoftDeletes;
    protected $fillable = [
        'image'
    ];
    public function descriptions($language_id= null)
    {

        if ($language_id) {
            $data = $this->hasMany(InstallmentDescription::class)->where('language_id', $language_id)->first();
            if(!$data){
                $data = $this->hasMany(InstallmentDescription::class)->first();
            }

            return $data;
        }
        $data = $this->hasMany(InstallmentDescription::class)->where('language_id', currentLanguage()['id'])->first();
        return $data;
    }


    public static function withDescription($installment_id = null)
    {
        $query = self::orderByDesc('installments.id')->join('installment_descriptions as cd', 'cd.installment_id', 'installments.id')
            ->where('cd.language_id', currentLanguage()->id)
            ->select(['installments.created_at', 'cd.*']);

        if ($installment_id)
        {
            if (is_array($installment_id))
            {
                $query->whereIn('installments.id', $installment_id);
            }
            else
            {
                $query->where('installments.id', $installment_id);
            }
        }

        $query->select([
            'installments.*',
            'cd.name',
        ]);
        return $query;
    }
}
