<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
class Airport extends Model
{

    protected $fillable = ['code', 'country_id', 'city_id'];
    use SoftDeletes;

    /**
     * Get description for pages
     * @return HasMany
     */
    public function descriptions() :HasMany
    {
        return $this->hasMany(AirportDescription::class);
    }

    /**
     * Get description of current language for pages
     * @return HasOne
     */
    public function currentDescription() :HasOne
    {
        return $this->hasOne(AirportDescription::class)->where('language_id', currentLanguage()->id);
    }


    public static function withDescription($region_id = null, $airport_id = null)
    {
        $airports = self::join('airport_descriptions as airDesc', 'airDesc.airport_id', 'airports.id')
            ->where('airDesc.language_id', currentLanguage()->id);

        if($region_id){
            $airports->where('airports.region_id', $region_id);
        }

        if ($airport_id){
            $airports->where('airports.id', $airport_id);
        }

        $airports->select([
            'airports.*',
            'airDesc.name',
        ]);
        return $airport_id ? $airports->first() : $airports->cursor() ?? [];
    }

}
