<?php

declare(strict_types=1);

namespace App\Framework\Vault\ValueObjects;

use InvalidArgumentException;

/**
 * Secret Key - Typsicherer Identifier für Vault Secrets
 *
 * Beispiele:
 * - database.password
 * - api.stripe.secret_key
 * - oauth.spotify.client_secret
 */
final readonly class SecretKey
{
    public function __construct(
        public string $value
    ) {
        if (empty($value)) {
            throw new InvalidArgumentException('Secret key cannot be empty');
        }

        if (strlen($value) > 255) {
            throw new InvalidArgumentException('Secret key too long (max 255 chars)');
        }

        // Nur alphanumerisch, dots, underscores, hyphens
        if (! preg_match('/^[a-zA-Z0-9._-]+$/', $value)) {
            throw new InvalidArgumentException(
                'Secret key can only contain letters, numbers, dots, underscores and hyphens'
            );
        }
    }

    public static function from(string $key): self
    {
        return new self($key);
    }

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

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