<?php

declare(strict_types=1);

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

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,
        );
    }
}
