<?php
/**
 * PHP 7.4 usage example for revealdi_submit().
 *
 * Staging endpoint: https://api.revealdi.com/v1/submissions
 * OBSERVE_ONLY: do not hide content, restore content, or take a profile offline.
 * Credentials are supplied separately. This file uses placeholders / getenv only.
 *
 * Run this from a RACF background job or queue worker, not from the web request
 * that already talks to Implio. API 201/200 is acceptance only. The evaluation
 * arrives later on observation.result.
 */

require_once __DIR__ . '/revealdi_observe_submit.php';

function racf_enqueue_revealdi_observe(array $event)
{
    // Call this from the queue consumer. Do not wait here for the webhook.
    $result = revealdi_submit(
        array(
            'client_submission_id' => $event['source_event_id'],
            'content_type' => 'profile_v1',
            'profile' => array(
                'display_name' => $event['display_name'],
                'bio' => $event['bio'],
            ),
            'assets' => $event['assets'],
            'external_ref' => array_filter(
                array(
                    'user_id' => $event['user_id'],
                    'item_id' => $event['item_id'],
                    'profile_revision' => $event['profile_revision'],
                    'username' => isset($event['username']) ? $event['username'] : null,
                    'email' => isset($event['email']) ? $event['email'] : null,
                    'action' => isset($event['action']) ? $event['action'] : null,
                ),
                static function ($value) {
                    return $value !== null && $value !== '';
                }
            ),
        ),
        array(
            'key_id' => getenv('REVEALDI_KEY_ID'),
            'secret' => getenv('REVEALDI_SECRET'),
            'base_url' => 'https://api.revealdi.com',
            'timeout' => 15,
        )
    );

    if (empty($result['ok'])) {
        // revealdi_submit already retried 502/reset/timeout with a new
        // Request-Id. Keep the same client_submission_id if a later job reruns.
        // Never treat a failed REVEALDI call as an Implio failure.
        return $result;
    }

    return $result;
}

// Placeholder payload for a controlled staging test. Replace with RACF fields.
if (PHP_SAPI === 'cli' && isset($argv[1]) && $argv[1] === '--example') {
    $demo = racf_enqueue_revealdi_observe(
        array(
            'source_event_id' => 'racf-evt-1001',
            'display_name' => 'Ada',
            'bio' => 'Hello from a controlled test.',
            'user_id' => '123456',
            'item_id' => 'item-88',
            'profile_revision' => '3',
            'username' => 'ada',
            'email' => 'ada@example.test',
            'assets' => array(
                array(
                    'role' => 'profile_image',
                    'image_id' => 'photo-a',
                    'source_url' => 'https://cdn.example.test/p/a.jpg',
                ),
                array(
                    'role' => 'profile_image',
                    'image_id' => 'photo-b',
                    'source_url' => 'https://cdn.example.test/p/b.jpg',
                ),
            ),
        )
    );
    echo isset($demo['http_code']) ? $demo['http_code'] : '0';
    echo "\n";
}
