<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Services\Plugin;

/**
 * 将远端可用性枚举与技术原因码映射为业务中文展示。
 */
final class HostPluginAvailabilityPresenter
{
    public const STATE_INSTALLABLE = 'installable';

    public const STATE_LICENSE_REQUIRED = 'license_required';

    public const STATE_INSTALLED = 'installed';

    public const STATE_UPDATE_AVAILABLE = 'update_available';

    public const STATE_UNAVAILABLE = 'unavailable';

    public const STATE_INCOMPATIBLE_VERSION = 'incompatible_version';

    public function labelForState(string $state): string
    {
        $key = 'dberp.plugin_center.catalog.state.'.$state;

        if (__($key) !== $key) {
            return (string) __($key);
        }

        return (string) __('dberp.plugin_center.catalog.state.unavailable');
    }

    public function badgeColorForState(string $state): string
    {
        return match ($state) {
            self::STATE_INSTALLABLE => 'success',
            self::STATE_UPDATE_AVAILABLE => 'info',
            self::STATE_INSTALLED => 'gray',
            self::STATE_LICENSE_REQUIRED => 'warning',
            self::STATE_INCOMPATIBLE_VERSION => 'warning',
            default => 'gray',
        };
    }

    public function remoteAvailabilityLabel(?string $availability): string
    {
        if ($availability === null || trim($availability) === '') {
            return (string) __('dberp.plugin_center.catalog.remote_availability.unknown');
        }

        $normalized = strtolower(trim($availability));
        $key = 'dberp.plugin_center.catalog.remote_availability.'.$normalized;

        if (__($key) !== $key) {
            return (string) __($key);
        }

        return (string) __('dberp.plugin_center.catalog.remote_availability.unknown');
    }

    public function reasonCodeLabel(?string $reasonCode): ?string
    {
        if ($reasonCode === null || trim($reasonCode) === '') {
            return null;
        }

        $normalized = strtolower(trim($reasonCode));
        $key = 'dberp.plugin_center.catalog.reason.'.$normalized;

        if (__($key) !== $key) {
            return (string) __($key);
        }

        return null;
    }

    public function blockedReasonLabel(?string $reasonCode, string $fallbackTranslationKey): string
    {
        $fromCode = $this->reasonCodeLabel($reasonCode);

        if ($fromCode !== null) {
            return $fromCode;
        }

        if ($this->isHumanReadableReason($reasonCode)) {
            return trim((string) $reasonCode);
        }

        return (string) __($fallbackTranslationKey);
    }

    public function isVersionIncompatibilityReason(?string $reasonCode): bool
    {
        if ($reasonCode === null || trim($reasonCode) === '') {
            return false;
        }

        $normalized = strtolower(trim($reasonCode));

        if (in_array($normalized, [
            'incompatible_version',
            'host_version_too_low',
            'version_incompatible',
            'host_version_below_minimum',
            'min_version_not_met',
        ], true)) {
            return true;
        }

        if ($this->isHumanReadableReason($reasonCode)) {
            foreach ([
                '宿主版本',
                '最低版本',
                '版本不兼容',
                '系统版本',
                'host version',
                'incompatible',
                'minimum version',
            ] as $pattern) {
                if (stripos($reasonCode, $pattern) !== false) {
                    return true;
                }
            }
        }

        return false;
    }

    private function isHumanReadableReason(?string $reasonCode): bool
    {
        if ($reasonCode === null || trim($reasonCode) === '') {
            return false;
        }

        $trimmed = trim($reasonCode);

        return ! preg_match('/^[a-z0-9_]+$/', strtolower($trimmed));
    }
}
