<?php

declare(strict_types=1);

namespace PhpWA\Client;

use PhpWA\Crypto\RandomSourceInterface;
use PhpWA\Crypto\SystemRandomSource;
use PhpWA\Exception\CryptoException;

final class RandomMessageIdGenerator implements MessageIdGeneratorInterface
{
    /** @var RandomSourceInterface */ private $random;
    public function __construct(?RandomSourceInterface $random = null) { $this->random = $random ?? new SystemRandomSource(); }
    public function generate(): string
    {
        $bytes = $this->random->bytes(18);
        if (strlen($bytes) !== 18) { throw new CryptoException('Unable to generate a WhatsApp message ID.'); }
        return '3EB0' . strtoupper(bin2hex($bytes));
    }
}
