<?php

namespace App\Models;

use App\Models\Concerns\HasUuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * A browser that has signed in before, and is therefore not news.
 *
 * Identified by a long-lived signed cookie carrying a random token, and by
 * nothing else. The two obvious alternatives both produce warnings nobody
 * reads: an IP changes on every train and every café, and a user-agent string
 * changes on every browser update — twelve false alarms a year, per customer,
 * from Chrome's release schedule alone.
 *
 * `name` is for the person reading the list. It is recomputed on every sign-in
 * so an upgraded browser relabels itself instead of raising an alarm, which
 * only works because the name is not what identifies anything.
 *
 * Scoped by guard as well as by id: operators and customers are two tables and
 * two guards (R21), and operator 1 is not customer 1.
 */
class UserDevice extends Model
{
    use HasUuid;

    protected $fillable = [
        'guard', 'authenticatable_id', 'token_hash', 'name', 'last_ip', 'last_seen_at',
    ];

    protected function casts(): array
    {
        return ['last_seen_at' => 'datetime'];
    }

    public function sessions(): HasMany
    {
        return $this->hasMany(LoginSession::class, 'device_id');
    }
}
