<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;
use OwenIt\Auditing\Auditable as AuditableTrait;


class User extends Authenticatable implements JWTSubject, Auditable
{
    use HasApiTokens, HasFactory, Notifiable, AuditableTrait;

    public function isAdmin(): bool
    {
      return $this->role === 'admin';
    }
    
    /**
     * The attributes that are NOT mass assignable.
     *
     */
    protected $guarded = [
        'deleted_at',
    ];

    /**
     * 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',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
        'password' => 'hashed',
    ];

    public function getJWTIdentifier()
    {
      return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
      return [
        'email'=>$this->email,
        'name'=>$this->name
      ];
    }
    protected $with = ['roles', 'employees', 'department_access', 'employee_profile_picture'];

    public function roles()
    {
        return $this->belongsToMany(
            Role::class,
            UserRole::class,
            'user_id',
            'role_id'
        );
    }
    public function department_access()
    {
        return $this->belongsToMany(
            Department::class,
            DepartmentAccess::class,
            'user_id',
            'department_id'
        );
    }
    public function employees(): BelongsTo
    {
      return $this->belongsTo(Employee::class, 'employee_id', 'employee_id');
    }

    public function employee_profile_picture(): BelongsTo
    {
      return $this->belongsTo(EmployeeProfilePicture::class, 'employee_id', 'employee_id')
        ->select(['employee_id', 'attachment']);
    }

    public function rooms()
    {
        return $this->hasMany(Room::class);
    }
}
