<?php

declare(strict_types=1);

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

namespace App\Exceptions\SupportCenter;

use RuntimeException;

/**
 * 更新回滚或恢复失败（不含密钥与 .env 内容）。
 */
final class HostSystemUpdateRecoveryException extends RuntimeException
{
    public function __construct(
        private readonly string $userFacingMessage,
        private readonly string $errorCode,
        ?\Throwable $previous = null,
    ) {
        parent::__construct($userFacingMessage, 0, $previous);
    }

    public function userFacingMessage(): string
    {
        return $this->userFacingMessage;
    }

    public function errorCode(): string
    {
        return $this->errorCode;
    }

    public static function rollbackNotAllowed(string $status): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.recovery.rollback_not_allowed', ['status' => $status]),
            errorCode: 'recovery_rollback_not_allowed',
        );
    }

    public static function backupUnavailable(): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.recovery.backup_unavailable'),
            errorCode: 'recovery_backup_unavailable',
        );
    }

    public static function fileRestoreFailed(string $reason): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.recovery.file_restore_failed', ['reason' => $reason]),
            errorCode: 'recovery_file_restore_failed',
        );
    }

    public static function envRestoreFailed(string $reason): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.recovery.env_restore_failed', ['reason' => $reason]),
            errorCode: 'recovery_env_restore_failed',
        );
    }

    public static function runtimeResetFailed(?\Throwable $previous = null): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.recovery.runtime_reset_failed'),
            errorCode: 'recovery_runtime_reset_failed',
            previous: $previous,
        );
    }

    public static function maintenanceCommandFailed(string $command, int $exitCode): self
    {
        $message = $command === 'up'
            ? (string) __('dberp.system_update.recovery.maintenance_up_failed_detail', [
                'exit_code' => (string) $exitCode,
            ])
            : (string) __('dberp.system_update.recovery.maintenance_command_failed', [
                'command' => $command,
                'exit_code' => (string) $exitCode,
            ]);

        return new self(
            userFacingMessage: $message,
            errorCode: 'recovery_maintenance_command_failed',
        );
    }

    public static function phpCliUnavailable(string $command): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.recovery.php_cli_unavailable', [
                'command' => $command,
            ]),
            errorCode: 'recovery_php_cli_unavailable',
        );
    }
}
