<?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 = ['price'];

    // 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 documentations()
    {
        return $this->hasMany(Documentation::class);
    }

    public static function withDescription($city_id = null, $hasRelation = null)
    {
        $cities = self::join('city_descriptions as cd', 'cd.city_id', 'cities.id')
            ->where('cd.language_id', currentLanguage()->id);

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

        if ($hasRelation){
            $cities->whereHas($hasRelation);
        }

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

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

}
