<?php

namespace Plugin\UserOnlineDevices;

use App\Models\User;
use App\Services\Plugin\AbstractPlugin;
use Illuminate\Support\Facades\URL;

class Plugin extends AbstractPlugin
{
    public function boot(): void
    {
        $this->filter('user.subscribe.response', function ($user) {
            if (!$user || empty($user['id'])) {
                return $user;
            }

            $ttlMinutes = max(1, (int) $this->getConfig('signed_url_ttl_minutes', 60));
            $userId = (int) $user['id'];

            $user['user_online_devices_enabled'] = true;
            $user['user_online_devices_panel_url'] = URL::temporarySignedRoute(
                'user-online-devices.panel',
                now()->addMinutes($ttlMinutes),
                ['user' => $userId]
            );
            $user['user_online_devices_snapshot_url'] = URL::temporarySignedRoute(
                'user-online-devices.snapshot',
                now()->addMinutes($ttlMinutes),
                ['user' => $userId]
            );

            return $user;
        });
    }
}
