<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class BioTimeDevice extends Model
{
    use HasFactory;

    protected $fillable = [
        'sn',
        'alias',
        'ip_address',
        'device_model',
        'area_id',
        'last_activity',
        'status',
        'is_active',
    ];

    protected $casts = [
        'last_activity' => 'datetime',
        'is_active' => 'boolean',
    ];

    public function isOnline(int $minutes = 5): bool
    {
        return (bool) ($this->last_activity?->gt(now()->subMinutes($minutes)));
    }

    public function getDisplayStatusAttribute(): string
    {
        return $this->isOnline() ? 'online' : 'offline';
    }

    public function area(): BelongsTo
    {
        return $this->belongsTo(Area::class);
    }
}
