<?php

namespace App\Models;

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

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;
    protected $fillable = ['name', 'email', 'password', 'password_changed_at', 'divisi_id', 'role', 'phone'];
    protected $hidden = ['password', 'remember_token'];
    protected $casts = [
        'password_changed_at' => 'datetime',
    ];

    public function mustChangePassword(): bool
    {
        if (!$this->password_changed_at) {
            return true;
        }

        return $this->password_changed_at->lte(now()->subMonths(3));
    }

    public function divisi()
    {
        return $this->belongsTo(Divisi::class);
    }
    public function borrowRequests()
    {
        return $this->hasMany(BorrowRequest::class);
    }
    public function isRole($role)
    {
        return $this->role === $role;
    }
    public function itemRequests()
    {
        return $this->hasMany(\App\Models\ItemRequest::class);
    }

    public function approvedItemRequests()
    {
        return $this->hasMany(\App\Models\ItemRequest::class, 'approved_by');
    }
}
