<?php

declare(strict_types=1);

namespace App\Framework\Http\Session;

use App\Framework\Random\RandomGenerator;

final readonly class SimpleSessionIdGenerator implements SessionIdGenerator
{
    public function __construct(
        private RandomGenerator $random,
        private int $length = 32
    ) {
    }

    /**
     * Generate a new session ID.
     *
     * @return SessionId New session ID value object
     */
    public function generate(): SessionId
    {
        $sessionIdValue = bin2hex($this->random->bytes($this->length));

        return SessionId::fromString($sessionIdValue);
    }

    /**
     * Validate that a session ID has the correct format.
     */
    public function isValid(SessionId $id): bool
    {
        $idString = $id->toString();

        return strlen($idString) === $this->length * 2 && ctype_xdigit($idString);
    }
}
