<?php

declare(strict_types=1);

namespace App\Domain\Auth;

/**
 * The four token kinds stored in api_tokens.
 *
 * The string value matches what is persisted in the `kind` column and
 * what flows through API contracts. The 3-letter code is the segment
 * embedded in the human-readable token string `irdb_<kind3>_<base32>`.
 */
enum TokenKind: string
{
    case Reporter = 'reporter';
    case Consumer = 'consumer';
    case Admin = 'admin';
    case Service = 'service';

    public function code(): string
    {
        return match ($this) {
            self::Reporter => 'rep',
            self::Consumer => 'con',
            self::Admin => 'adm',
            self::Service => 'svc',
        };
    }

    public static function fromCode(string $code): ?self
    {
        return match ($code) {
            'rep' => self::Reporter,
            'con' => self::Consumer,
            'adm' => self::Admin,
            'svc' => self::Service,
            default => null,
        };
    }
}
