<?php

declare(strict_types=1);

namespace App\Services\SupportCenter;

/**
 * 官方授权权益 → 面向 DBErp 业务的中文展示（阶段 1 不强调主题/模板权益）。
 */
final class HostSystemAuthorizationEntitlementPresenter
{
    /**
     * @var list<string>
     */
    private const PRIMARY_ENTITLEMENT_KEYS = [
        'can_check_system_update',
        'can_execute_system_update',
        'can_install_plugin',
        'can_update_plugin',
    ];

    /**
     * @return list<array{key: string, label: string, enabled: bool}>
     */
    public function primaryEntitlementsForDisplay(?array $entitlements): array
    {
        if ($entitlements === null || $entitlements === []) {
            return [];
        }

        $rows = [];

        foreach (self::PRIMARY_ENTITLEMENT_KEYS as $key) {
            if (! array_key_exists($key, $entitlements)) {
                continue;
            }

            $rows[] = [
                'key' => $key,
                'label' => $this->labelFor($key),
                'enabled' => (bool) $entitlements[$key],
            ];
        }

        return $rows;
    }

    public function labelFor(string $key): string
    {
        $translationKey = 'dberp.system_authorization.entitlements.'.$key;
        $label = __($translationKey);

        return $label !== $translationKey ? $label : (string) __('dberp.system_authorization.entitlements.unknown');
    }
}
