<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

/**
 * App\User
 *
 * @property int $id
 * @property string $organisation
 * @property string $first_name
 * @property string $last_name
 * @property string $full_name
 * @property string $date_of_birth
 * @property string $phone_number
 * @property string $website_url
 * @property string $username
 * @property string $email
 * @property string $password
 * @property int $role_id
 * @property string $created_by
 * @property int $wrong_password_attempt_count
 * @property bool $is_locked
 * @property bool $must_change_password
 * @property string $remember_token
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property \Carbon\Carbon $deleted_at
 * @property string $status
 * @property string $pass
 * @property string $keyword
 * @property string $location
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\AuditTrail[] $audit_trails
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Contact[] $contacts
 * @property-read \App\Credit $credit
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\CreditHistory[] $creditHistory
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\ContactGroup[] $groups
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\UserJob[] $jobs
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Log[] $logs
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\PendingSingleSms[] $pending_single_sms
 * @property-read \App\Picture $picture
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\QueueMessage[] $queue_messages
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\RejectedSms[] $rejected_sms
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Report[] $reports
 * @property-read \App\Role $role
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\SmsImage[] $sms_images
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\TmpGroupSms[] $tmp_group_sms
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\UserNetwork[] $user_networks
 * @method static \Illuminate\Database\Query\Builder|\App\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereCreatedBy($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereDateOfBirth($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereEmail($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereFirstName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereFullName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereIsLocked($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereKeyword($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereLastName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereLocation($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereMustChangePassword($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereOrganisation($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User wherePass($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User wherePassword($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User wherePhoneNumber($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereRememberToken($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereRoleId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereStatus($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereUsername($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereWebsiteUrl($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereWrongPasswordAttemptCount($value)
 * @mixin \Eloquent
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\User onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\User withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\User withoutTrashed()
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\PendingSingleEmail[] $pending_single_email
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Audience[] $audiences
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\EmailJob[] $email_jobs
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\EmailLog[] $email_logs
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\TmpGroupEmail[] $tmp_group_emails
 * @property-read \App\UserCurrency $userCurrency
 * @property-read \Currency|\Illuminate\Database\Eloquent\Model|static $currency
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\StanbicUser[] $stambic_users
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\StanbicUser[] $stanbic_users
 */
class User extends Authenticatable
{
    use Notifiable, SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'email', 'password', 'organisation', 'first_name', 'last_name', 'full_name', 'date_of_birth', 'location','sender_id','is_locked','company_id',
        'phone_number', 'website_url', 'username', 'role_id', 'created_by', 'status', 'pass', 'must_change_password','avatar','google_id','name','use_promotion'
    ];

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

    public function byPhone($phone_number)
    {
        return $this->newQuery()->where('phone_number',$phone_number)->first();
    }

    public function byPhoneNumber($phone_number)
    {
        return $this->newQuery()->where('phone_number', $phone_number)->first();
    }

    protected $dates = ['deleted_at'];

    protected $appends = ['currency'];

    /**
     * @return Currency
     */

    public function getCurrencyAttribute()
    {
        $temp = $this->userCurrency ? $this->userCurrency->currency : Currency::whereDefault(true)->firstOrFail();
        return $temp->abbr;
    }

    public function socialCredit()
    {
        return $this->hasOne(SocialMediaCredit::class);
    }

    public function role() {
        return $this->belongsTo('App\Role');
    }

    public function company()
    {
        return $this->belongsTo(Company::class);
    }


    public function sender_ids()
    {
        return $this->belongsTo(UserSenderId::class);
    }


    public function userSenderIds()
    {
        return $this->hasMany(UserSenderId::class);
    }

    public function social()
    {
        return $this->hasMany(UserSocial::class);

    }

    public function trail($message)
    {
        $message = ucwords($message);

        AuditTrail::query()->create([
            'user_id'   => $this->id,
            'username'  => $this->username,
            'date'      => Carbon::now()->toDateTimeString(),
            'activity'  => $this->first_name.' '.$this->last_name. ": {$message}"
        ]);
    }

    public function posts()
    {
        return $this->hasMany(UserPost::class);
    }


    public function admin_access_rights()
    {
        return $this->hasOne('App\AdminAccessRight');
    }

    public function userCurrency() {
        return $this->hasOne('App\UserCurrency');
    }

    public function picture() {
        return $this->hasOne('App\Picture');
    }

    public function user_networks() {
        return $this->hasMany('App\UserNetwork');
    }

    public function credit() {
        return $this->hasOne('App\Credit', 'user_id');
    }

    public function creditHistory() {
        return $this->hasMany('App\CreditHistory');
    }

    public function jobs() {
        return $this->hasMany('App\UserJob');
    }

    public function email_jobs() {
        return $this->hasMany('App\EmailJob');
    }

    public function contacts() {
        return $this->hasMany('App\Contact');
    }

    public function tmp_group_sms() {
        return $this->hasMany('App\TmpGroupSms');
    }

    public function tmp_group_emails() {
        return $this->hasMany('App\TmpGroupEmail');
    }

    public function groups() {
        return $this->hasMany('App\ContactGroup');
    }

    public function rejected_sms() {
        return $this->hasMany('App\RejectedSms');
    }

    public function pending_single_sms() {
        return $this->hasMany('App\PendingSingleSms');
    }

    public function pending_single_email() {
        return $this->hasMany('App\PendingSingleEmail');
    }

    public function reports() {
        return $this->hasMany('App\Report');
    }

    public function audit_trails() {
        return $this->hasMany('App\AuditTrail');
    }

    public function sms_images() {
        return $this->hasMany('App\SmsImage');
    }

    public function sms_log() {
        return $this->hasMany('App\Log');
    }

    public function email_logs() {
        return $this->hasMany('App\EmailLog');
    }

    public function queue_messages() {
        return $this->hasMany('App\QueueMessage');
    }

    public function audiences() {
        return $this->hasMany('App\Audience');
    }

    public function stanbic_users() {
        return $this->hasMany('App\StanbicUser');
    }

}
