<?php

namespace App\Models;

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

class Brand extends Model
{
    //
    use SoftDeletes;
    protected $fillable = [
        'image'
    ];
    public function descriptions($language_id= null)
    {
        if ($language_id) {
            $data = $this->hasMany(BrandDescription::class)->where('language_id', $language_id)->first();
            if(!$data){
                $data = $this->hasMany(BrandDescription::class)->first();
            }

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


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

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

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