<?php

namespace App\Models\Core\Auth;

use Sebastienheyd\Boilerplate\Models\User as parentUser;
// use HighIdeas\UsersOnline\Traits\UsersOnlineTrait;
use Yadahan\AuthenticationLog\AuthenticationLogable;

class User extends parentUser
{
    // use UserHasTeams, UsersOnlineTrait;
    use AuthenticationLogable;

    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',
        'settings' => 'array',
    ];

    public function passwordSecurity()
    {
        return $this->hasOne(PasswordSecurity::class);
    }

    public function teams()
    {
        return $this->belongsToMany(Team::class, TeamUser::class);
    }

    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);
    }
}
