<?php

declare(strict_types=1);

namespace App\Administration\Domain\Model\Invitation;

use App\Administration\Domain\Exception\InvitationCodeInvalideException;

use function ctype_xdigit;
use function hash_equals;
use function strlen;

/**
 * Value Object representing a cryptographically secure invitation code.
 *
 * Codes are 32-character hexadecimal strings generated from random_bytes(16).
 */
final readonly class InvitationCode
{
    private const int EXPECTED_LENGTH = 32;

    /** @var non-empty-string */
    public string $value;

    public function __construct(string $value)
    {
        if (strlen($value) !== self::EXPECTED_LENGTH || !ctype_xdigit($value)) {
            throw InvitationCodeInvalideException::pourCode($value);
        }

        $this->value = $value;
    }

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

    /**
     * @return non-empty-string
     */
    public function __toString(): string
    {
        return $this->value;
    }
}
