<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
#[Fillable(['name', 'username', 'email', 'password', 'role', 'logo', 'is_active', 'password_must_change', 'password_reset_code', 'password_reset_expires_at'])]
#[Hidden(['password', 'remember_token', 'password_reset_code'])]
class User extends Authenticatable
{
/** @use HasFactory<UserFactory> */
use HasFactory, HasUuids, Notifiable;
/**
* Minutes an OTP code stays valid for the "lupa password" flow.
*/
public const PASSWORD_RESET_CODE_EXPIRES_MINUTES = 15;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'is_active' => 'boolean',
'password_must_change' => 'boolean',
'password_reset_expires_at' => 'datetime',
];
}
public function isAdmin(): bool
{
return $this->role === 'admin';
}
public function isTechnician(): bool
{
return $this->role === 'technician';
}
/**
* Generate a fresh 6-digit OTP for the "lupa password" flow and persist it.
*/
public function generatePasswordResetCode(): string
{
$code = str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);
$this->update([
'password_reset_code' => $code,
'password_reset_expires_at' => now()->addMinutes(self::PASSWORD_RESET_CODE_EXPIRES_MINUTES),
]);
return $code;
}
public function isPasswordResetCodeExpired(): bool
{
return $this->password_reset_expires_at === null || now()->greaterThan($this->password_reset_expires_at);
}
public function clearPasswordResetCode(): void
{
$this->update(['password_reset_code' => null, 'password_reset_expires_at' => null]);
}
/**
* Get the responses written by this technician.
*/
public function responses(): HasMany
{
return $this->hasMany(Respons::class, 'technician_id');
}
/**
* Get the complaints currently assigned to this technician.
*/
public function assignedComplaints(): HasMany
{
return $this->hasMany(Complaint::class, 'assigned_technician_id');
}
}