<?php

declare(strict_types=1);

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];
    }
}
