<?php

declare(strict_types=1);

namespace apexl\Io\modules\user\services;

use apexl\Io\modules\user\exceptions\JwtException;
use apexl\Io\modules\user\Factories\JwtFromRequestServiceFactory;
use Psr\Http\Message\ServerRequestInterface;

final readonly class SessionInfoFromJwtTokenService
{
    public function __construct(private JwtFromRequestServiceFactory $jwtFromRequestServiceFactory) {}

    /**
     * @throws JwtException
     */
    public function info(ServerRequestInterface $request)
    {
        $token = $this->jwtFromRequestServiceFactory->make($request)->token();

        [, $encodedSessionInfo] = explode('.', $token);

        return json_decode(base64_decode($encodedSessionInfo));

    }
}