<?php

declare(strict_types=1);

namespace App\Services\SupportCenter;

use App\Enums\SupportCenter\HostSystemUpdateExecutionStep;
use App\Models\HostSystemUpdateBackup;
use App\Models\HostSystemUpdateExecution;

/**
 * 数据库回滚边界：DBErp 迁移可能不可逆，本阶段不自动 migrate:rollback。
 */
final class HostSystemUpdateDatabaseRollbackBoundary
{
    public function hasDatabaseSnapshotBackup(HostSystemUpdateExecution $execution): bool
    {
        return $execution->backups()
            ->where('backup_type', HostSystemUpdateBackup::TYPE_DATABASE_SNAPSHOT)
            ->where('status', HostSystemUpdateBackup::STATUS_COMPLETED)
            ->exists();
    }

    public function requiresManualDatabaseRecovery(HostSystemUpdateExecution $execution): bool
    {
        return $execution->current_step === HostSystemUpdateExecutionStep::RunMigrations->value
            && ! $this->hasDatabaseSnapshotBackup($execution);
    }

    public function manualDatabaseRecoveryMessage(): string
    {
        return (string) __('dberp.system_update.recovery.database_manual_recovery_required');
    }
}
