<?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 Team extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable = [
        'image'
    ];


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

        $query->select([
            'teams.*',
            'sd.name',
            'sd.description',
        ]);
        return $query;
    }
    public function descriptions($language_id= null)
    {
        if ($language_id) {
            $data = $this->hasMany(TeamDescription::class)->where('language_id', $language_id)->first();
            if(!$data){
                $data = $this->hasMany(TeamDescription::class)->first();
            }
            return $data;
        }
        $data = $this->hasMany(TeamDescription::class)->where('language_id', currentLanguage()['id'])->first();
        return $data;
    }
}
