<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Partner extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable = [
        'image'
    ];

    public function getUrlImageAttribute()
    {
        return asset($this->image);
    }

    public function descriptions($language_id= null)
    {
        if ($language_id) {
            $data = $this->hasMany(PartnerDescription::class)->where('language_id', $language_id)->first();
            if(!$data){
                $data = $this->hasMany(PartnerDescription::class)->first();
            }
            return $data;
        }
        $data = $this->hasMany(PartnerDescription::class)->where('language_id', currentLanguage()['id'])->first();
        return $data;
    }


    public static function withDescription($partner_id = null)
    {
        $query = self::orderByDesc('partners.id')->join('partner_descriptions as pd', 'pd.partner_id', 'partners.id')
            ->where('pd.language_id', currentLanguage()->id)
            ->select(['partners.created_at', 'pd.*']);
        if ($partner_id){
            $query->where('partners.id', $partner_id);
        }

        $query->select([
            'partners.image',
            'pd.name',
        ]);
        return $query;
    }
}
