<?php

declare(strict_types=1);

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

namespace App\Enums\SupportCenter;

/**
 * Web 分步更新阶段（每阶段对应一次 run-next-step 请求）。
 */
enum HostSystemUpdateStepPhase: string
{
    case BootstrapCheck = 'bootstrap_check';
    case BootstrapDownload = 'bootstrap_download';
    case BootstrapParse = 'bootstrap_parse';
    case BootstrapCreateExecution = 'bootstrap_create_execution';
    case MaintenanceDown = 'maintenance_down';
    case Precheck = 'precheck';
    case ExtractStaging = 'extract_staging';
    case Backup = 'backup';
    case ReplaceFiles = 'replace_files';
    case ReplaceVendor = 'replace_vendor';
    case ResetRuntime = 'reset_runtime';
    case ClearBootstrapCache = 'clear_bootstrap_cache';
    case RuntimeReadiness = 'runtime_readiness';
    case MigrateBatch = 'migrate_batch';
    case ClearCache = 'clear_cache';
    case PostUpdateHealthCheck = 'post_update_health_check';
    case WriteVersion = 'write_version';
    case CommitVendorBackup = 'commit_vendor_backup';
    case Complete = 'complete';

    public function next(): ?self
    {
        return match ($this) {
            self::BootstrapCheck => self::BootstrapDownload,
            self::BootstrapDownload => self::BootstrapParse,
            self::BootstrapParse => self::BootstrapCreateExecution,
            self::BootstrapCreateExecution => self::Precheck,
            self::Precheck => self::MaintenanceDown,
            self::MaintenanceDown => self::ExtractStaging,
            self::ExtractStaging => self::Backup,
            self::Backup => self::ReplaceFiles,
            self::ReplaceFiles => self::ReplaceVendor,
            self::ReplaceVendor => self::ResetRuntime,
            self::ResetRuntime => self::ClearBootstrapCache,
            self::ClearBootstrapCache => self::RuntimeReadiness,
            self::RuntimeReadiness => self::MigrateBatch,
            self::MigrateBatch => self::ClearCache,
            self::ClearCache => self::PostUpdateHealthCheck,
            self::PostUpdateHealthCheck => self::WriteVersion,
            self::WriteVersion => self::CommitVendorBackup,
            self::CommitVendorBackup => self::Complete,
            self::Complete => null,
        };
    }

    public function isBootstrap(): bool
    {
        return match ($this) {
            self::BootstrapCheck,
            self::BootstrapDownload,
            self::BootstrapParse,
            self::BootstrapCreateExecution => true,
            default => false,
        };
    }

    public function labelKey(): string
    {
        return 'dberp.system_update_center.step_phases.'.$this->value;
    }
}
