<?php

namespace App\Models;

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

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

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

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


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

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

        $query->select([
            'shippings.*',
            'cd.name',
        ]);
        return $query;
    }
    public function currentDescription()
    {
        return $this->hasOne(ShippingDescription::class)->where('language_id',currentLanguage()['id']);
    }
}
