<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Laravel\Scout\Searchable;

/**
 * Class Developer
 *
 * @package App
 * @property string $name
 * @property string $code
 * @property tinyInteger $enabled
 * @property string $description
 * @property string $logotype_icon
 * @property string $banner
 * @property string $build_text
 * @property string $being_built_text
 * @property string $logotype_color
 * @property string $email
 * @property string $phone
 * @property integer $main_page_priority
 * @property integer $developer_page_priority
 * @property string $birthday
 * @property string $build
 * @property string $being_built
 * @property string $meta_title
 * @property string $meta_keywords
 * @property string $meta_description
 * @property string $seo_text
 * @property string $facebook
 * @property string $youtube
 * @property string $site_link
 * @property string $city
 * @property-read string $year
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @property-write mixed $city_id
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Developer newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Developer newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Developer onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Developer query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\Developer withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Developer withoutTrashed()
 * @mixin \Eloquent
 */
class Developer extends Model
{
    use SoftDeletes, Notifiable, Searchable;

    const PAGINATE = 20;
    const CACHE_KEY_MAIN_PAGE_RESULT = "developer_main_page_result";
    const CACHE_KEY_MAIN_PAGE_COUNT = "developer_main_page_count";

    protected $fillable = [
        'name',
        'code',
        'enabled',
        'description',
        'logotype_icon',
        'email',
        'phone',
        'main_page_priority',
        'developer_page_priority',
        'birthday',
        'being_built',
        'build',
        'meta_title',
        'meta_keywords',
        'meta_description',
        'seo_text',
        'being_built_text',
        'build_text',
        'facebook',
        'site_link',
        'city_id',
        'logotype_color',
        'banner'
    ];
    protected $hidden = [];
    protected $appends = ['year'];

    /**
     * Set attribute to money format
     * @param $input
     */
    public function setMainPagePriorityAttribute($input)
    {
        $this->attributes['main_page_priority'] = $input ? $input : null;
    }

    /**
     * Set attribute to money format
     * @param $input
     */
    public function setDeveloperPagePriorityAttribute($input)
    {
        $this->attributes['developer_page_priority'] = $input ? $input : null;
    }

//    /**
//     * Set attribute to date format
//     * @param $input
//     */
//    public function setBirthdayAttribute($input)
//    {
//        if ($input != null && $input != '') {
//            $this->attributes['birthday'] = Carbon::createFromFormat(config('app.date_format'),
//                $input)->format('Y-m-d');
//        } else {
//            $this->attributes['birthday'] = null;
//        }
//    }
//
//    /**
//     * Get attribute from date format
//     * @param $input
//     *
//     * @return string
//     */
//    public function getBirthdayAttribute($input)
//    {
//        $zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format'));
//        if ($input != $zeroDate && $input != null) {
//            return Carbon::createFromFormat('Y-m-d', $input)->format(config('app.date_format'));
//        } else {
//            return '';
//        }
//    }

//    /**
//     * Get attribute from date format
//     * @param $input
//     *
//     * @return string
//     */
//    public function getYearAttribute($input)
//    {
//        $zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format'));
//        $year = $this->birthday;
//        if ($year != $zeroDate && $year != null) {
//            return Carbon::createFromFormat(config('app.date_format'), $year)->format('Y');
//        } else {
//            return '';
//        }
//    }

    /**
     * Set to null if empty
     * @param $input
     */
    public function setCityIdAttribute($input)
    {
        $this->attributes['city_id'] = $input ? $input : null;
    }

    public function city()
    {
        return $this->belongsTo(City::class, 'city_id')->withTrashed();
    }

    public function residentialComplex()
    {
        $this->belongsToMany(ResidentialComplex::class)->withTrashed();
    }

    /**
     * Get the index name for the model.
     *
     * @return string
     */
    public function searchableAs()
    {
        return config("scout.developer_index");
    }

    /**
     * Get the indexable data array for the model.
     *
     * @return array
     */
    public function toSearchableArray()
    {
        $array = [
            'id' => $this->id,
            'name' => $this->name,
            'enabled' => (int)$this->enabled,
            'city' => $this->city ? $this->city->name : ""
        ];

        return $array;
    }

    public function shouldBeSearchable()
    {
        return $this->enabled == 1 ? true : false;
    }
}
