<?php

namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

/**
 * A single CPU+MEM metric sample for a server, pushed to the browser over Reverb.
 * Dev: emitted by `clusev:mock-metrics`. Later: by MetricsPoller (SSH).
 */
class MetricsTicked implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public function __construct(
        public int $cpu,
        public int $mem = 0,
        public string $server = 'web-01',
    ) {}

    public function broadcastOn(): PrivateChannel
    {
        // PRIVATE: requires /broadcasting/auth (see routes/channels.php). Infrastructure
        // metrics must not be subscribable by anyone holding the public app key.
        return new PrivateChannel('metrics');
    }

    public function broadcastAs(): string
    {
        return 'tick';
    }

    /** @return array<string,mixed> */
    public function broadcastWith(): array
    {
        return ['cpu' => $this->cpu, 'mem' => $this->mem, 'server' => $this->server];
    }
}
