<?php

declare(strict_types=1);

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

namespace App\Services\SupportCenter;

use App\Models\HostSystemUpdatePackage;

/**
 * 更新包下载与 manifest 状态 → 中文业务标签。
 */
final class HostSystemUpdatePackageStatusPresenter
{
    public function downloadStatusLabel(?string $downloadStatus): string
    {
        $normalized = strtolower(trim((string) $downloadStatus));

        if ($normalized === '') {
            return (string) __('dberp.system_update_center.package.download_status.not_downloaded');
        }

        $translationKey = 'dberp.system_update_center.package.download_status.'.$normalized;
        $label = __($translationKey);

        return $label !== $translationKey
            ? $label
            : (string) __('dberp.system_update_center.package.download_status.unknown');
    }

    public function downloadStatusBadgeColor(?string $downloadStatus): string
    {
        return match (strtolower(trim((string) $downloadStatus))) {
            HostSystemUpdatePackage::STATUS_VERIFIED,
            HostSystemUpdatePackage::STATUS_DOWNLOADED => 'success',
            HostSystemUpdatePackage::STATUS_DOWNLOADING,
            HostSystemUpdatePackage::STATUS_TOKEN_ISSUED => 'info',
            HostSystemUpdatePackage::STATUS_VERIFICATION_FAILED,
            HostSystemUpdatePackage::STATUS_FAILED => 'danger',
            default => 'gray',
        };
    }

    public function checksumStatusLabel(bool $verified): string
    {
        return $verified
            ? (string) __('dberp.system_update_center.package.checksum.verified')
            : (string) __('dberp.system_update_center.package.checksum.not_verified');
    }

    public function checksumBadgeColor(bool $verified): string
    {
        return $verified ? 'success' : 'warning';
    }

    public function manifestStatusLabel(?string $manifestStatus): string
    {
        $normalized = strtolower(trim((string) $manifestStatus));

        if ($normalized === '') {
            return (string) __('dberp.system_update_center.package.manifest.not_parsed');
        }

        $translationKey = 'dberp.system_update_center.package.manifest.'.$normalized;
        $label = __($translationKey);

        return $label !== $translationKey
            ? $label
            : (string) __('dberp.system_update_center.package.manifest.unknown');
    }

    public function manifestBadgeColor(?string $manifestStatus): string
    {
        return match (strtolower(trim((string) $manifestStatus))) {
            HostSystemUpdatePackage::MANIFEST_STATUS_VALID => 'success',
            HostSystemUpdatePackage::MANIFEST_STATUS_INVALID,
            HostSystemUpdatePackage::MANIFEST_STATUS_PARSE_FAILED,
            HostSystemUpdatePackage::MANIFEST_STATUS_UNSUPPORTED => 'danger',
            default => 'gray',
        };
    }

    public function formatBytes(?int $bytes): ?string
    {
        if ($bytes === null || $bytes <= 0) {
            return null;
        }

        $units = ['B', 'KB', 'MB', 'GB'];
        $value = (float) $bytes;
        $unitIndex = 0;

        while ($value >= 1024 && $unitIndex < count($units) - 1) {
            $value /= 1024;
            $unitIndex++;
        }

        return number_format($value, $unitIndex === 0 ? 0 : 2).' '.$units[$unitIndex];
    }
}
