<?php

declare(strict_types=1);

namespace App\Framework\LiveComponents\ValueObjects;

use InvalidArgumentException;

/**
 * Upload Session Identifier
 *
 * Eindeutige ID für eine Chunked-Upload-Session.
 * Format: Kryptographisch sicherer 32-Zeichen String.
 */
final readonly class UploadSessionId
{
    private const MIN_LENGTH = 32;

    public function __construct(
        public string $value
    ) {
        if (strlen($value) < self::MIN_LENGTH) {
            throw new InvalidArgumentException(
                sprintf('Upload session ID must be at least %d characters', self::MIN_LENGTH)
            );
        }

        if (!preg_match('/^[a-zA-Z0-9]+$/', $value)) {
            throw new InvalidArgumentException('Upload session ID must be alphanumeric');
        }
    }

    /**
     * Create from existing string
     */
    public static function fromString(string $value): self
    {
        return new self($value);
    }

    public function equals(self $other): bool
    {
        return hash_equals($this->value, $other->value);
    }

    public function toString(): string
    {
        return $this->value;
    }

    public function __toString(): string
    {
        return $this->value;
    }
}
