<?php

namespace App\Models;

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

class Device extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'name',
        'hostname',
        'os_info',
        'local_ip',
        'last_ip',
        'pairing_code',
        'pairing_expires_at',
        'device_token_hash',
        'online',
        'last_seen_at',
    ];

    protected $casts = [
        'online' => 'boolean',
        'pairing_expires_at' => 'datetime',
        'last_seen_at' => 'datetime',
    ];

    protected $hidden = [
        'device_token_hash',
        'pairing_code',
    ];

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

    public function isPairable(): bool
    {
        return $this->pairing_code !== null
            && $this->pairing_expires_at !== null
            && $this->pairing_expires_at->isFuture()
            && $this->user_id === null;
    }
}
