<?php

declare(strict_types=1);

namespace Plugin\tnt_mcp\src\Mcp\Action;

/**
 * Where the legal values of a field come from, so `describe_admin_action` can hand the LLM a list
 * instead of a sentence asking it to go and look. Two sources, never both: a shop table with an id
 * and a label column, or a named service for the sets that are not rows (ISO country codes,
 * languages).
 */
final class EnumSpec
{
    private function __construct(
        public readonly ?string $table,
        public readonly ?string $id,
        public readonly ?string $label,
        public readonly ?string $service,
    ) {
    }

    public static function fromTable(string $table, string $id, string $label): self
    {
        return new self($table, $id, $label, null);
    }

    public static function fromService(string $service): self
    {
        return new self(null, null, null, $service);
    }

    /**
     * @param array<mixed> $raw
     */
    public static function fromArray(array $raw): self
    {
        if (\is_string($raw['service'] ?? null)) {
            return self::fromService($raw['service']);
        }

        return self::fromTable(
            \is_string($raw['table'] ?? null) ? $raw['table'] : '',
            \is_string($raw['id'] ?? null) ? $raw['id'] : '',
            \is_string($raw['label'] ?? null) ? $raw['label'] : '',
        );
    }

    public function isService(): bool
    {
        return $this->service !== null;
    }
}
