<?php

namespace App\Provisioning\Jobs;

use App\Support\Settings;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

/**
 * A sign of life from the provisioning worker.
 *
 * The scheduler enqueues it, the worker executes it — so it proves exactly what
 * the scheduler heartbeat (routes/console.php) does NOT prove: that someone is
 * actually pulling jobs off the `provisioning` queue. The two fail
 * independently, and "scheduler up, worker down" is the silent case: paid
 * orders pile up on the queue, and nothing reports anything, because the
 * scheduler itself is doing exactly what it is supposed to do.
 *
 * Written through Settings, not the cache: a heartbeat that vanishes on a Redis
 * restart would report an outage that never happened, which is worse than no
 * check at all — nobody trusts it after the second false alarm.
 */
class RecordProvisioningHeartbeat implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct()
    {
        $this->onQueue('provisioning');
    }

    public function handle(): void
    {
        Settings::set('heartbeat.queue_provisioning', now()->toIso8601String());
    }
}
