<?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 HostSystemUpdatePostApplyException extends RuntimeException
{
    public function __construct(
        private readonly string $userFacingMessage,
        private readonly string $errorCode,
        private readonly bool $requiresRollback = true,
        ?\Throwable $previous = null,
    ) {
        parent::__construct($userFacingMessage, 0, $previous);
    }

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

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

    public function requiresRollback(): bool
    {
        return $this->requiresRollback;
    }

    public static function commandNotAllowed(string $command): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.post_apply.command_not_allowed', ['command' => $command]),
            errorCode: 'post_apply_command_not_allowed',
            requiresRollback: false,
        );
    }

    public static function commandUnavailable(string $command): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.post_apply.command_unavailable', ['command' => $command]),
            errorCode: 'post_apply_command_unavailable',
            requiresRollback: true,
        );
    }

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

    public static function commandFailed(string $command, int $exitCode): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.post_apply.command_failed', [
                'command' => $command,
                'exit_code' => (string) $exitCode,
            ]),
            errorCode: 'post_apply_command_failed',
            requiresRollback: true,
        );
    }

    public static function migrationFailed(?\Throwable $previous = null): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.post_apply.migration_failed'),
            errorCode: 'post_apply_migration_failed',
            requiresRollback: true,
            previous: $previous,
        );
    }

    public static function runtimeResetFailed(string $reason): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.post_apply.runtime_reset_failed', ['reason' => $reason]),
            errorCode: 'post_apply_runtime_reset_failed',
            requiresRollback: true,
        );
    }

    public static function runtimeReadinessFailed(string $reason): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.post_apply.runtime_readiness_failed', ['reason' => $reason]),
            errorCode: 'post_apply_runtime_readiness_failed',
            requiresRollback: true,
        );
    }

    public static function healthCheckFailed(string $reason): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.post_apply.health_check_failed', ['reason' => $reason]),
            errorCode: 'post_apply_health_check_failed',
            requiresRollback: true,
        );
    }

    public static function cacheClearFailed(string $command): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.post_apply.cache_clear_failed', ['command' => $command]),
            errorCode: 'post_apply_cache_clear_failed',
            requiresRollback: true,
        );
    }

    public static function versionWriteFailed(string $reason): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.post_apply.version_write_failed', ['reason' => $reason]),
            errorCode: 'post_apply_version_write_failed',
            requiresRollback: true,
        );
    }

    public static function manifestVersionMissing(): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.post_apply.manifest_version_missing'),
            errorCode: 'post_apply_manifest_version_missing',
            requiresRollback: true,
        );
    }
}
