<?php

declare(strict_types=1);

namespace App\Support\Baselines\Compare;

use InvalidArgumentException;
use Stringable;

final class CompareStrategyKey implements Stringable
{
    public readonly string $value;

    public function __construct(string $value)
    {
        $normalized = trim(mb_strtolower($value));

        if ($normalized === '' || ! preg_match('/^[a-z0-9_]+$/', $normalized)) {
            throw new InvalidArgumentException('Compare strategy keys must be non-empty lowercase snake_case strings.');
        }

        $this->value = $normalized;
    }

    public static function from(self|string $value): self
    {
        return $value instanceof self ? $value : new self($value);
    }

    public static function intunePolicy(): self
    {
        return new self('intune_policy');
    }

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

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