<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'firstname',
        'lastname',
        'email',
        'password',
        'role',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
    ];

    public static function users()
    {
        return self::where('role', 4)->get();
    }

    public static function admins()
    {
        return self::whereIn('role', [1,2,3])->get();
    }

    public static function fullAdminsCount()
    {
        return self::whereIn('role', [1])->count();
    }

    public function jobRole()
    {
        return $this->hasOne(Department::class, 'id', 'jobRole');
    }

    public function toolsSetup()
    {
        return $this->HasMany(UserToolSetup::class, 'user_id', 'id');
    }

    public function userTools()
    {
        $formattedArray = [];
        foreach ($this->toolsSetup()->get() as $toolSetup) {
            $tool = $toolSetup->tool()->first();
            if (!empty($tool)) {
                $formattedArray[] = [
                    'ID'    => $tool->id,
                    'name'  => $tool->name,
                    'status'=> $toolSetup->status,
                ];
            }
            
        }

        return $formattedArray;
    }
}
