<?php

namespace App\Models\Core\Auth;

use Carbon\Carbon;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\SoftDeletes;
use Yadahan\AuthenticationLog\AuthenticationLogable;
// use HighIdeas\UsersOnline\Traits\UsersOnlineTrait;
use Sebastienheyd\Boilerplate\Models\User as parentUser;

class User extends parentUser
{
    // use UserHasTeams, UsersOnlineTrait;
    use AuthenticationLogable, SoftDeletes;
 
    protected $fillable = ['active', 'last_name', 'first_name', 'username', 'email', 'password', 'remember_token', 'last_login'];
    protected $hidden = ['password', 'remember_token'];
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function passwordSecurity()
    {
        return $this->hasOne('App\Models\Core\Auth\PasswordSecurity');
    }

    public function teams()
    {
        return $this->hasManyThrough(Team::class, TeamUser::class, 'user_id', 'id', 'id', 'team_id');
    }

    public function scopeByTeam($query, $id)
    {
        return $query->whereHas('teams', function ($query) use ($id) {
            $query->where('id', $id);
        });
    }

    public function scopeByUniqueTeam($query)
    {
        return $query->has('teams', '=', 1);
    }

    public function scopeActive($query)
    {
        return $query->where('active', 1);
    }
}
