<?php

namespace App\Models;

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

class Device extends Model
{
    use HasFactory;

    protected $fillable = [
        'site_id', 'name', 'token_hash', 'last_seen', 'config_version',
        'last_queue_depth', 'camera_ok', 'agent_version',
    ];

    protected function casts(): array
    {
        return [
            'last_seen' => 'datetime',
            'camera_ok' => 'boolean',
        ];
    }

    public function site()
    {
        return $this->belongsTo(Site::class);
    }

    public function attendanceEvents()
    {
        return $this->hasMany(AttendanceEvent::class);
    }

    public function isOnline(): bool
    {
        return $this->last_seen !== null && $this->last_seen->gt(now()->subMinutes(5));
    }
}
