<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

use App\Repositories\UserRepository;
use App\Http\Traits\CustomerId;

/**
 * Class User
 *
 * @package App\Models
 */
class User extends Authenticatable
{

    const ROOT_STORAGE_FILES = 'users_images/';

    use Notifiable;
    use CustomerId;

    /**
     * The table field that stores the primary key.
     *
     * @var string
     */
    protected $primaryKey = 'oid';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [ 'userid', 'firstname', 'lastname', 'mobile', 'picture', 'is_employee' ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [ 'pivot' ];

    /**
     * Boot
     */
    public static function boot()
    {
        parent::boot();

        User::updated(function ($user) {
            // Delete old file on profile picture renew
            if ($user->isDirty('picture') && $user->getOriginal('picture')) {
                \File::delete(storage_path('users_images/' . CustomerId::getCustomerId() . '/' . $user->getOriginal('picture')));
            }
        });
    }

    /**
     * The users repository
     *
     * @return \App\Repositories\UserRepository
     */
    public function repository()
    {
        return UserRepository::instance($this);
    }

    /**
     * The groups where the user belongs.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function groups()
    {
        return $this->belongsToMany('App\Models\Group', 'user2group', 'useroid', 'groupoid');
    }

    /**
     * User sessions
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function sessions()
    {
        return $this->hasMany('App\Models\UserSession', 'user_oid', 'oid');
    }

    /**
     * Users messages
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function messages()
    {
        return $this->hasMany('App\Models\Message', 'useroid', 'oid');
    }

    /**
     * Fcm tokens
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function fcmTokens()
    {
        return $this->hasMany('App\Models\FcmToken', 'useroid', 'oid');
    }

    /**
     * Full name
     *
     * @return string
     */
    public function fullname()
    {
        return $this->firstname . ' ' . $this->lastname;
    }

    /**
     * Get users avatar
     *
     * @param array $params
     * @return string
     */
    public function getAvatar(array $params = [])
    {
        $sendParams = [
            'customerid' => CustomerId::getCustomerId(),
            'width'      => $params['width'],
            'height'     => $params['height'],
            'filename'   => $this->picture
        ];

        return ($this->picture) ? route('apiShowProfilePicture', $sendParams) : '';
    }

    /**
     * The info outputted on API calls
     *
     * @param array $extra
     * @return array
     */
    public function apiOutputData($extra = [])
    {
        $data = [
            'oid'         => $this->oid,
            'userid'      => $this->userid,
            'firstname'   => $this->firstname,
            'lastname'    => $this->lastname,
            'mobile'      => $this->mobile,
            'picture'     => $this->getAvatar([ 'width' => 180, 'height' => 180 ]),
            'is_employee' => ($this->is_employee) ? true : false
        ];

        return array_merge($data, $extra);
    }
}
