<?php

namespace App\Models;

use Illuminate\Support\Str;

class PropertyPlace extends BaseModel
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'property_place';
    protected $appends = ['languageNameArray','slug'];

    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';

    protected $dateFormat = 'U';
    protected $guarded = [];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [];

    /**
     * @return mixed
     */

    function getLanguageNameArrayAttribute()
    {
        $languageName = [];
        if (!empty($this->language_name)) {
            $languageName = json_decode($this->language_name, 1);
        }
        return $languageName;
    }

    function getSlugAttribute()
    {
        return '/place/' . Str::slug($this->name, $separator = '-', $language = 'en') . '-' . $this->id;
    }

    //$propertyPlace['route'] = '/place/' . Str::slug($propertyPlace['name'], $separator = '-', $language = 'en') . '-' . $propertyPlace['id'];

    public function propertyPlaceCategory()
    {
        return $this->hasOne('App\Models\PropertyPlaceCategory','id', 'place_category_id')
            ->where('status', '=', 1)
            ->select("id", "name", "language_key", "parent_id", "level", "order_number","icon","status");
    }

    public function propertyPlaceWorkingHour()
    {
        return $this->hasOne('App\Models\PropertyPlaceWorkingHour','id', 'place_working_hour_id')
            ->where('status', '=', 1)
            ->select("id", "name", "language_key", "status");
    }

    public function propertyPlacePhotoMapping()
    {
        return $this->hasMany('App\Models\PropertyPlacePhotoMapping','place_id', 'id') ;
    }

    public function propertyPlaceFactMapping()
    {
        return $this->hasMany('App\Models\PropertyPlaceFactMapping','property_place_id', 'id') ;
    }

    public function propertyPlaceCategoryFieldValue()
    {
        return $this->hasMany('App\Models\PropertyPlaceCategoryFieldValue','place_id', 'id') ;
    }

    public function propertyWebPlaceMapping()
    {
        return $this->hasMany('App\Models\PropertyWebPlaceMapping', 'property_place_id', 'id')->select(['id', 'property_id', 'property_web_id', 'property_place_id']);
    }

}
