<?php

declare(strict_types=1);

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 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(): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.post_apply.migration_failed'),
            errorCode: 'post_apply_migration_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,
        );
    }
}
