<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
 * @property mixed umrah_trip_date
 */
class UmrahTrip extends Model
{
    protected $fillable = [
//        'umrah_trip_type',
        'rate',
        'house_type',
        'price_per_person',
        'umrah_trip_date',
        'departure_city',
        'arrival_city',
        'rally_point_info',
        'image',
        'status',
    ];

    protected $casts = ['rally_point_info' => 'array', 'umrah_trip_date' => 'array'];

    public function descriptions() :HasMany
    {
        return $this->hasMany(UmrahTripDescription::class);
    }

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

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

    public static function withDescription($umrah_trip_id = null)
    {
        $languageId = currentLanguage()->id;
        $umrahTrips = self::join('umrah_trip_descriptions as umrahDesc', 'umrahDesc.umrah_trip_id', 'umrah_trips.id')
            ->where('umrahDesc.language_id', $languageId);


        if ($umrah_trip_id){
            $umrahTrips->where('umrah_trips.id', $umrah_trip_id);
        }

        $umrahTrips->select(
            [
                'umrah_trips.*',
                'umrahDesc.name',
            ]
        );

        return $umrah_trip_id ? $umrahTrips->first() : $umrahTrips->get();

    }

}
