<?php

declare(strict_types=1);

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

namespace App\Services\SupportCenter;

use App\Enums\SupportCenter\HostSystemUpdateStepPhase;
use App\Models\HostSystemUpdateExecution;

/**
 * 重阶段分批执行编排入口。
 */
final class HostSystemUpdateBatchPhaseRunner
{
    public function __construct(
        private readonly HostSystemUpdateExtractBatchRunner $extractBatchRunner,
        private readonly HostSystemUpdateBackupBatchRunner $backupBatchRunner,
        private readonly HostSystemUpdateFileReplaceBatchRunner $fileReplaceBatchRunner,
        private readonly HostSystemUpdateVendorReplaceBatchRunner $vendorReplaceBatchRunner,
        private readonly HostSystemUpdateExecutionBatchProgressStore $progressStore,
    ) {}

    public function runNextBatch(
        HostSystemUpdateExecution $execution,
        HostSystemUpdateStepPhase $phase,
    ): HostSystemUpdateBatchPhaseResult {
        $execution = $execution->fresh(['package', 'backups']) ?? $execution;

        $result = match ($phase) {
            HostSystemUpdateStepPhase::ExtractStaging => $this->extractBatchRunner->runNextBatch($execution),
            HostSystemUpdateStepPhase::Backup => $this->backupBatchRunner->runNextBatch($execution),
            HostSystemUpdateStepPhase::ReplaceFiles => $this->fileReplaceBatchRunner->runNextBatch($execution),
            HostSystemUpdateStepPhase::ReplaceVendor => $this->vendorReplaceBatchRunner->runNextBatch($execution),
            default => throw new \InvalidArgumentException('Unsupported batch phase.'),
        };

        $this->progressStore->save((string) $execution->execution_no, $result->progress);

        return $result;
    }

    public function isPhaseComplete(HostSystemUpdateExecution $execution, HostSystemUpdateStepPhase $phase): bool
    {
        $progress = $this->snapshotProgress($execution, $phase);

        return $progress?->isCompleted() === true;
    }

    public function snapshotProgress(
        HostSystemUpdateExecution $execution,
        HostSystemUpdateStepPhase $phase,
    ): ?HostSystemUpdateBatchProgress {
        return match ($phase) {
            HostSystemUpdateStepPhase::ExtractStaging => $this->extractBatchRunner->snapshotProgress($execution),
            HostSystemUpdateStepPhase::Backup => $this->backupBatchRunner->snapshotProgress($execution),
            HostSystemUpdateStepPhase::ReplaceFiles => $this->fileReplaceBatchRunner->snapshotProgress($execution),
            HostSystemUpdateStepPhase::ReplaceVendor => $this->vendorReplaceBatchRunner->snapshotProgress($execution),
            default => null,
        };
    }

    public function clearPhaseProgress(HostSystemUpdateExecution $execution, HostSystemUpdateStepPhase $phase): void
    {
        $this->progressStore->clear((string) $execution->execution_no, $phase->value);
    }
}
