<?php

declare(strict_types=1);

namespace EasyChat\Library\Update;

final class UpdatePlatformProfile
{
     
    public static function current(): array
    {
        $edition = defined('EASYCHAT_WPORG_MODE') && EASYCHAT_WPORG_MODE
            ? 'wordpress_org'
            : strtolower(trim((string)(defined('EASYCHAT_CMS_EDITION') ? EASYCHAT_CMS_EDITION : 'standalone')));

        $profiles = [
            'standalone' => ['Generic PHP', ['generic'], 'generic', 'managed_runtime', 'EasyChat'],
            'generic' => ['Generic PHP', ['generic'], 'generic', 'managed_runtime', 'EasyChat'],
            'opencart' => ['OpenCart', ['opencart_oc23_oc3', 'opencart_oc4'], '', 'cms_native', 'установщик OpenCart'],
            'wordpress' => ['WordPress', ['wordpress'], 'wordpress', 'cms_native', 'раздел «Плагины» WordPress'],
            'wordpress_org' => ['WordPress.org', [], '', 'cms_store_native', 'раздел «Плагины» WordPress'],
            'webasyst' => ['Webasyst', ['webasyst'], 'webasyst', 'cms_native', '«Инсталлер» Webasyst'],
            'webasyst_store' => ['Webasyst', [], '', 'cms_store_native', '«Инсталлер» Webasyst'],
            'bitrix' => ['1С-Битрикс', ['bitrix'], 'bitrix', 'cms_native', 'система обновлений 1С-Битрикс'],
        ];
        if (!isset($profiles[$edition])) {
            $edition = 'standalone';
        }
        [$label, $allowed, $primary, $mode, $nativeLabel] = $profiles[$edition];

        return [
            'id' => $edition,
            'label' => $label,
            'allowed_artifact_keys' => $allowed,
            'primary_artifact_key' => $primary,
            'management_mode' => $mode,
            'native_label' => $nativeLabel,
            'managed_install' => $mode === 'managed_runtime',
        ];
    }

    public static function allowsArtifact(array $profile, string $key): bool
    {
        return in_array($key, $profile['allowed_artifact_keys'] ?? [], true);
    }
}
