<?php

declare(strict_types=1);

namespace App\Services\SupportCenter;

/**
 * 系统更新执行状态 → 中文业务标签。
 */
final class HostSystemUpdateStatusPresenter
{
    public function label(?string $status): string
    {
        $normalized = strtolower(trim((string) $status));

        if ($normalized === '') {
            return (string) __('dberp.system_update_center.status.unknown');
        }

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

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

    public function badgeColor(?string $status): string
    {
        return match (strtolower(trim((string) $status))) {
            'completed', 'rolled_back' => 'success',
            'running', 'prechecking', 'rolling_back' => 'info',
            'ready', 'pending' => 'gray',
            'rollback_required' => 'warning',
            'failed', 'rollback_failed', 'cancelled' => 'danger',
            default => 'gray',
        };
    }

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

        return $this->label($rollbackStatus);
    }

    public function checkStatusLabel(?string $checkStatus): string
    {
        return match (strtolower(trim((string) $checkStatus))) {
            'success' => (string) __('dberp.system_update_center.check_status.success'),
            'failed' => (string) __('dberp.system_update_center.check_status.failed'),
            default => (string) __('dberp.system_update_center.check_status.never'),
        };
    }

    public function checkStatusBadgeColor(?string $checkStatus): string
    {
        return match (strtolower(trim((string) $checkStatus))) {
            'success' => 'success',
            'failed' => 'danger',
            default => 'gray',
        };
    }
}
