<?php

/**
 * Example: one message, multiple groups, different actions.
 *
 * Run this file in THREE separate terminals, once per group:
 *     php consume_multi_group.php email-service
 *     php consume_multi_group.php analytics-service
 *     php consume_multi_group.php audit-service
 *
 * Then publish one message with produce.php. Because each terminal uses a
 * different group.id, ALL THREE receive the same message and each performs
 * a different action.
 */

require_once __DIR__ . '/../autoload.php';

$group = $argv[1] ?? 'email-service';

// Map each group to the action it performs on the same message.
$actions = [
    'email-service'     => fn(array $m) => printf("[email]     sending mail for: %s\n", $m['payload']),
    'analytics-service' => fn(array $m) => printf("[analytics] recording metric for: %s\n", $m['payload']),
    'audit-service'     => fn(array $m) => printf("[audit]     writing audit log for: %s\n", $m['payload']),
];

$action = $actions[$group] ?? fn(array $m) => printf("[%s] %s\n", $group, $m['payload']);

$config   = new KafkaConfig('127.0.0.1:9092');
$consumer = new Consumer($config, $group, ['orders']);

echo "Consumer group '{$group}' listening on 'orders'... (Ctrl+C to stop)\n";

// Stop cleanly on Ctrl+C.
pcntl_async_signals(true);
pcntl_signal(SIGINT, static fn() => $consumer->stop());

$consumer->consume($action);
$consumer->close();
