<?php

declare(strict_types=1);

namespace App\Services\Install;

/**
 * 安装进度页面向业务人员展示的阶段名称（与 {@see WebInstallPhase} 技术阶段对应）。
 */
final class WebInstallProgressDisplay
{
    public const MISSING_TASK_MESSAGE = '安装任务不存在或已过期，请重新开始安装。';

    public const MISSING_PAYLOAD_MESSAGE = '安装任务数据已过期或不完整，请重新开始安装。';

    public const INSTALL_HEADLINE = '正在安装 DBErp';

    /** @deprecated Use {@see self::INSTALL_HEADLINE} */
    public const PREPARING_HEADLINE = self::INSTALL_HEADLINE;

    public const WAITING_FOR_EXECUTION_LABEL = '等待执行初始化步骤';

    public const COMPLETED_USER_MESSAGE = 'DBErp 已安装完成。你现在可以进入系统开始使用。';

    public const WARNING_USER_MESSAGE = 'DBErp 已完成基础安装，但部分收尾检查需要处理。这通常不会影响登录和基础使用。';

    public const FAILED_USER_MESSAGE = '安装未完成。请根据失败阶段提示检查配置后重试。';

    public const NETWORK_RETRY_MESSAGE = '网络连接暂时不稳定，系统将自动重试获取安装进度…';

    /**
     * 安装进度页「安装步骤」清单（面向业务人员）。
     * 不含 {@see WebInstallPhase::RunningOptionalFinalizers}：后台仍会执行存储链接等可选收尾，不在此列表展示。
     *
     * @return list<array{key: string, label: string}>
     */
    public static function businessStages(): array
    {
        return [
            ['key' => WebInstallPhase::CheckingDatabase->value, 'label' => '检查数据库状态'],
            ['key' => WebInstallPhase::WritingEnv->value, 'label' => '写入系统配置'],
            ['key' => WebInstallPhase::Migrating->value, 'label' => '创建数据表'],
            ['key' => WebInstallPhase::CreatingAdmin->value, 'label' => '创建超级管理员'],
            ['key' => WebInstallPhase::UpdatingSystemProfile->value, 'label' => '初始化系统档案'],
            ['key' => WebInstallPhase::WritingInstallMarkers->value, 'label' => '写入安装标记'],
        ];
    }

    public static function businessLabelForStage(string $stageKey): string
    {
        foreach (self::businessStages() as $stage) {
            if ($stage['key'] === $stageKey) {
                return $stage['label'];
            }
        }

        if ($stageKey === WebInstallPhase::Completed->value) {
            return '安装完成';
        }

        return '';
    }

    public static function statusLabelZh(WebInstallProgressStatus $status): string
    {
        return match ($status) {
            WebInstallProgressStatus::Pending => self::WAITING_FOR_EXECUTION_LABEL,
            WebInstallProgressStatus::Running => '正在执行安装步骤',
            WebInstallProgressStatus::Warning => '安装已完成，存在需关注的提示',
            WebInstallProgressStatus::Failed => '安装失败',
            WebInstallProgressStatus::Completed => '安装已完成',
        };
    }
}
