<?php

declare(strict_types=1);

namespace App\Framework\Database\Indexing\ValueObjects;

/**
 * Value Object representing a database index name
 */
final readonly class IndexName
{
    public function __construct(
        public string $value
    ) {
        if (empty($value)) {
            throw new \InvalidArgumentException('Index name cannot be empty');
        }

        if (strlen($value) > 64) {
            throw new \InvalidArgumentException('Index name cannot exceed 64 characters');
        }

        // Validate index name format (alphanumeric + underscore)
        if (!preg_match('/^[a-zA-Z0-9_]+$/', $value)) {
            throw new \InvalidArgumentException(
                'Index name must contain only alphanumeric characters and underscores'
            );
        }
    }

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

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

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