<?php declare(strict_types=1);
/**
 * This file is part of the SplendidBear Websites' projects.
 *
 * Copyright (c) 2026 @ www.splendidbear.org
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\Service;

use Firebase\JWT\JWT;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

/**
 * Class MercureJwtService
 *
 * Mints Mercure subscriber JWTs carrying an identifying payload so the hub's
 * /subscriptions endpoint can report which known player is connected.
 *
 * @package  App\Service
 * @author   Lang <https://www.splendidbear.org>
 * @category Class
 * @license  https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
 * @link     www.splendidbear.org
 * @since    2026. 04. 19.
 */
final readonly class MercureJwtService
{
    public function __construct(
        #[Autowire(env: 'MERCURE_JWT_SECRET')]
        private string $secret,
    ) {
    }

    public function mintSubscriberToken(string $gameAssoc, string $userName): string
    {
        return JWT::encode(
            [
                'mercure' => [
                    'subscribe' => ['*'],
                    'payload'   => [
                        'username'  => $userName,
                        'gameAssoc' => $gameAssoc,
                    ],
                ],
            ],
            $this->secret,
            'HS256'
        );
    }
}
