<?php

namespace App\Console\Commands;

use App\Events\DashboardStatsUpdated;
use App\Services\Monitoring\SystemMonitoringService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class BroadcastDashboardStats extends Command
{
    protected $signature = 'dashboard:broadcast-stats';

    protected $description = 'Broadcast live system stats to admin.monitoring channel';

    public function handle(SystemMonitoringService $monitor): void
    {
        // Bust the cache so we always get fresh data for the broadcast
        Cache::forget('monitoring_full_bundle');

        $stats = $monitor->getAll();

        // Slim payload — only what the dashboard widgets need
        $payload = [
            'cpu'        => $stats['cpu'],
            'ram'        => $stats['ram'],
            'disk'       => $stats['disk'],
            'users'      => $stats['users'],
            'queues'     => $stats['queues'],
            'uptime'     => $stats['uptime'],
            'has_reverb' => $stats['has_reverb'],
            'last_update'=> $stats['last_update'],
        ];

        DashboardStatsUpdated::dispatch($payload);
    }
}
