<?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 City extends Model
{
    // Fields that mass assignment
    protected $fillable = ['country_id'];

    // Use soft delete
    use SoftDeletes;

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

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

    public function country()
    {
        return $this->belongsTo(Country::class);
    }

    public function hotelReservations()
    {
        return $this->hasMany(HotelReservation::class);
    }

    public function umrahTripReservations()
    {
        return $this->hasMany(UmrahTripReservation::class);
    }

    public function airports()
    {
        return $this->hasMany(Airport::class);
    }
    public static function withDescription($ocuntry_id = null, $city_id = null)
    {
        $cities = self::join('city_descriptions as cd', 'cd.city_id', 'cities.id')
            ->where('cd.language_id', currentLanguage()->id);

        if($ocuntry_id){
            $cities->where('cities.country_id', $ocuntry_id);
        }

        if ($city_id){
            $cities->where('cities.id', $city_id);
        }

        $cities->select([
            'cities.*',
            'cd.name',
        ]);
        return $city_id ? $cities->first() : $cities->cursor() ?? [];
    }

}
