<?php

declare(strict_types=1);

namespace Duplicator\Addons\ProBase\Telemetry;

use Duplicator\Core\UniqueId;

/**
 * Daily fetch of pre-computed nudges from the worker.
 *
 * The fetch only primes a transient; UI consumption (banners, dismissal,
 * frequency capping) is out of scope for this release per the plan.
 */
class TelemetryNudges
{
    const CACHE_TRANSIENT = 'dupli_opt_telemetry_nudges';
    const CACHE_TTL       = DAY_IN_SECONDS;

    /**
     * Cron handler. Caches the response for 24h.
     *
     * @return void
     */
    public static function cronFetch(): void
    {
        if (!TelemetryClient::isEnabled()) {
            return;
        }

        $payload = TelemetryClient::get(TelemetryClient::ROUTE_NUDGES, [
            'identifier' => UniqueId::getInstance()->getIdentifier(),
        ]);

        if ($payload === null) {
            return;
        }

        // SECURITY: $payload originates from the worker. Any future UI
        // consumption must escape it on output (esc_html / wp_kses) — the
        // transient is treated as untrusted input.
        set_transient(self::CACHE_TRANSIENT, $payload, self::CACHE_TTL);
    }

    /**
     * Read the cached nudges, or null when none have been fetched.
     *
     * @return ?array<string, mixed>
     */
    public static function getCachedNudges(): ?array
    {
        $cached = get_transient(self::CACHE_TRANSIENT);
        return is_array($cached) ? $cached : null;
    }
}
