<?php

namespace App;

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

/**
 * @property mixed id
 */

class Package extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable = [
        'image',
        'video',
        'video_type',
        'package_category_id',
        'user_id',
        'visit_counts',
        'meta',
    ];

    /**
     * descrips how the model cast the field value
     */
    protected $casts = ['meta' => 'array'];
    
    /**
     * Get the Attach
     *
     * @return string
     */
    public function getAttachAttribute(): ?string
    {
        return $this->meta ? $this->meta['attach'] ?? null : null;
    }
    
    /**
     * Get the price
     *
     * @return float
     */
    public function getPriceAttribute(): float
    {
        return $this->meta ? $this->meta['price'] ?? 0 : 0;
    }
    
    /**
     * Get the old price
     *
     * @return float
     */
    public function getOldPriceAttribute(): float
    {
        return $this->meta ? $this->meta['old_price'] ?? 0 : 0;
    }
    
    /**
     * Get the persons
     *
     * @return int
     */
    public function getPersonsAttribute(): int
    {
        return $this->meta ? $this->meta['persons'] ?? 0 : 0;
    }
    
    /**
     * Get the wifi
     *
     * @return bool
     */
    public function getWifiAttribute(): bool
    {
        return $this->meta ? $this->meta['wifi'] ?? false : false;
    }

    /**
     * Get the flight_duration
     *
     * @return string
     */
    public function getFlightDurationAttribute(): ?string
    {
        return $this->meta ? $this->meta['flight_duration'] ?? null: null;
    }
        
    /**
     * Get the start_date
     *
     * @return string
     */
    public function getStartDateAttribute(): ?string
    {
        return $this->meta ? $this->meta['start_date'] ?? null: null;
    }
    
    /**
     * Get the end_date
     *
     * @return string
     */
    public function getEndDateAttribute(): ?string
    {
        return $this->meta ? $this->meta['end_date'] ?? null: null;
    }
    
    /**
     * Get the latitude
     *
     * @return string
     */
    public function getLatitudeAttribute(): ?string
    {
        return $this->meta ? $this->meta['latitude'] ?? null: null;
    }
    
    /**
     * Get the longitude
     *
     * @return string
     */
    public function getLongitudeAttribute(): ?string
    {
        return $this->meta ? $this->meta['longitude'] ?? null: null;
    }

    /**
     * Get the Wifi text
     *
     * @return string
     */
    public function getWifiTextAttribute(): string
    {
        return $this->wifi ? __('main.available') : __('main.not_available');
    }

    /**
     * Get the flight_duration text
     *
     * @return string
     */
    public function getFlightDurationTextAttribute(): ?string
    {
        if ($this->flight_duration) {
            return __('packages.flight_duration',['hours'=> $this->flight_duration]);
        }
        return null;
    }
    
    /**
     * Get the Journey Duration Text
     *
     * @return string
     */
    public function getJourneyDurationTextAttribute(): string
    {
        if ($this->start_date && $this->end_date) {
            $dateTo = new \DateTime($this->end_date);
            $dateFrom = new \DateTime($this->start_date);
            // $dateDiff = $dateFrom->diff($dateTo);
            $dateDiff = $dateTo->diff($dateFrom);
            return __('packages.journey_duration',['days'=> $dateDiff->days, 'nights'=> ($dateDiff->days - 1)]);
        }
        return '---';
    }
    
    /**
     * Get the Journey Period Text
     *
     * @return string
     */
    public function getJourneyPeriodTextAttribute(): string
    {
        if ($this->start_date && $this->end_date) {
            return  date(' d M ', strtotime($this->start_date)) . ' - ' .date(' d M ', strtotime($this->end_date)) ;
        }
        return '---';
    }
    
    /**
     * Check expire
     *
     * @return bool
     */
    public function getIsExpireAttribute(): bool
    {
        if ( $this->end_date) {
            return  strtotime($this->end_date) < time() ;
        }
        return false;
    }

    /**
     * Return Main Photo of package
     * @return string|null
     */
    public function getMainPhotoAttribute() : ?string
    {
        $mainPhoto= $this->image ?? null;
        if( !$mainPhoto ){
            $photo= $this->photos()->first();
            $mainPhoto = $photo ? $photo->image : null;
        }
        $mainPhoto = getImage($mainPhoto);
        return $mainPhoto;
    }

    /**
     * Get the Attach URL
     *
     * @return string
     */
    public function getAttachUrlAttribute(): ?string
    {
        return getFile($this->attach) ?? null;
    }

    /**
     * Return Slug of package
     * @return string|null
     */
    public function getSlugTextAttribute() : ?string
    {
        return str_slug(strtolower($this->currentDescription->slug));
    }

    /**
     * Return Url of package
     * @return string|null
     */
    public function getUrlAttribute() : ?string
    {
        return route('packages.details',[ 'package'=> $this->id, 'slug'=> $this->slug_text]);
    }

    /**
     * Return Packages Url of package
     * @return string|null
     */
    public function getPackagesUrlAttribute() : ?string
    {
        return route('packages.details',[ 'package'=> $this->id, 'slug'=> $this->slug_text]);
    }

    /**
     * Return current descriptions of package
     * @return HasOne
     */
    public function currentDescription() : HasOne
    {
        return $this->hasOne(PackageDescription::class)->where('language_id', currentLanguage()->id);
    }

    /**
     * Return descriptions for package
     * @return HasMany
     */
    public function descriptions() : HasMany
    {
        return $this->hasMany(PackageDescription::class);
    }

    /**
     * Return category for package
     * @return BelongsTo
     */
    public function category() :BelongsTo
    {
        return $this->belongsTo(PackageCategory::class);
    }


    /**
     * Return other photos for package
     * @return HasMany
     */
    public function photos () :HasMany
    {
        return $this->hasMany(PackagePhoto::class);
    }

}
