<?php

declare(strict_types=1);

namespace App\Exceptions\SupportCenter;

use RuntimeException;

/**
 * 更新 staging 解压或备份失败（面向用户的文案，不含密钥与 .env 内容）。
 */
final class HostSystemUpdateStagingException 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 packageNotReady(): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.staging.package_not_ready'),
            errorCode: 'staging_package_not_ready',
        );
    }

    public static function packageFileMissing(): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.staging.package_file_missing'),
            errorCode: 'staging_package_file_missing',
        );
    }

    public static function zipOpenFailed(): self
    {
        return self::archiveOpenFailed();
    }

    public static function archiveOpenFailed(): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.staging.archive_open_failed'),
            errorCode: 'staging_archive_open_failed',
        );
    }

    public static function unsupportedArchiveFormat(string $format): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.staging.unsupported_archive_format', [
                'format' => $format,
            ]),
            errorCode: 'staging_unsupported_archive_format',
        );
    }

    public static function unsafeArchiveEntry(string $entry): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.staging.unsafe_archive_entry'),
            errorCode: 'staging_unsafe_archive_entry',
        );
    }

    public static function protectedUpdateTarget(string $path, string $context = 'path'): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.staging.protected_update_target', [
                'path' => $path,
                'context' => $context,
            ]),
            errorCode: 'staging_protected_update_target',
        );
    }

    public static function stagingVerificationFailed(string $reason): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.staging.verification_failed', ['reason' => $reason]),
            errorCode: 'staging_verification_failed',
        );
    }

    public static function backupFailed(string $reason): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.backup.failed', ['reason' => $reason]),
            errorCode: 'backup_failed',
        );
    }

    public static function precheckFailed(string $message, string $errorCode): self
    {
        return new self(
            userFacingMessage: $message,
            errorCode: $errorCode,
        );
    }
}
