<?php

namespace App\Models\Auth;

use App\Models\Auth\Traits\Attribute\UserAttribute;
use App\Models\Auth\Traits\Method\UserMethod;
use App\Models\Auth\Traits\Relationship\UserRelationship;
use App\Models\Auth\Traits\Scope\UserScope;
use App\Models\Boards\Board;
use Illuminate\Support\Facades\Auth;

/**
 * Class User.
 */
class User extends BaseUser
{
    use UserAttribute,
        UserMethod,
        UserRelationship,
        UserScope;

    // prefix lookup relation
    public function prefix()
    {
        return $this->belongsTo('App\Models\Lookups\Prefix_lookup', 'prefix_lookup_id', 'id');
    }

    // position lookup relation
    public function position()
    {
        return $this->belongsTo('App\Models\Lookups\Position_lookup', 'position_lookup_id', 'id');
    }

    // institution lookup relation
    public function institution()
    {
        return $this->belongsTo('App\Models\Lookups\Institution_lookup', 'institution_lookup_id', 'id');
    }

    public function labs()
    {
        return $this->belongsToMany('App\Models\Lab', 'users_labs', 'user_id', 'lab_id')->withPivot('admin','moderator')->withTimestamps();
    }

    // a user can have many folders
    public function board_folders()
    {
        return $this->hasMany('App\Models\Boards\BoardFolder', 'user_id', 'id')->orderBy('order');
    }

    public function GetPlanAttribute()
    {
        $labStr = '';
        if ($this->is_in_lab) {
            $labStr = ', lab member';
        }
        return ($this->subscriptions->first() ? $this->subscriptions->first()->name : 'Free plan') . $labStr;
    }


    // get the first lab that this user is admin on.
    //20200525 but only if they've got an appropriate role Will need to be a bit careful about this...
    public function GetMyLabAttribute()
    {
        if ($this->hasRole(['lab admin', 'big lab admin', 'administrator'])) {
            return $this->labs()->where(['user_id' => $this->id, 'admin' => 'true'])->first();
        }else{
            return false;
        }

    }

    // get any old labs that a user might be admin in
    public function GetMyOldLabsAttribute()
    {
            return $this->labs()->where(['user_id' => $this->id, 'admin' => 'true'])->get()->count()>0?$this->labs()->where(['user_id' => $this->id, 'admin' => 'true'])->get():false;
    }

    public function GetIsInLabAttribute()
    {
        // dd('hi');
        return $this->labs()->count() > 0;
    }

    public function GetHasSubscriptionAttribute()
    {
        return $this->subscriptions->count() > 0;
    }

    public function GetHasActiveSubscriptionAttribute()
    {
        return $this->subscriptions()->active()->get()->count() > 0;
    }

    public function GetCurrentActiveSubscriptionsAttribute()
    {
        return $this->subscriptions()->active()->get();
    }

    public function GetCurrentIncompleteSubscriptionsAttribute()
    {
        return $this->subscriptions()->incomplete()->get();
    }

    // invites that this user has pending
    public function invites()
    {
        return $this->hasMany('App\Models\Auth\Invite', 'created_by', 'id');
    }

    // invites that this user has pending
    public function pending_data()
    {
        return $this->hasMany('App\Models\Auth\UserPendingData', 'user_id', 'id');
    }

    public function boards()
    {
        return $this->hasMany('App\Models\Boards\Board', 'user_id', 'id');
    }

    // Does this user have pending invitations?
    public function GetMyBoardsCountAttribute()
    {
        return $this->boards->whereNull('lab_id')->whereNull('exclude_from_limit')->count();
    }

    public function user_uploaded_images()
    {
        return $this->hasMany('App\Models\Boards\UserUploadedImage', 'user_id', 'id');
    }

    public function user_uploaded_backgrounds()
    {
        return $this->hasMany('App\Models\Graphics\BackgroundImage', 'user_id', 'id');
    }


    // Does this user have pending invitations?
    public function GetCountInvitedAttribute()
    {
        return $this->invites()->where(['email' => $this->email])->count();
    }

    // Does this user have an active paid account? (or is an admin)
    public function GetisPaidAttribute()
    {
        return $this->isAdmin() || ($this->subscriptions()->first() ? ($this->subscriptions()->first()->stripe_status == 'active') : false);
//return $this->subscriptions()->first();
    }


    //@TODO get count of forum posts, mentions etc.


}
