<?php

declare(strict_types=1);

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

namespace App\Exceptions\SupportCenter;

use RuntimeException;

/**
 * 系统更新执行任务创建或状态流转失败（面向用户的文案，不含密钥）。
 */
final class HostSystemUpdateExecutionException 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 packageNotVerified(): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.execution.package_not_verified'),
            errorCode: 'execution_package_not_verified',
        );
    }

    public static function manifestNotParsed(): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.execution.manifest_not_parsed'),
            errorCode: 'execution_manifest_not_parsed',
        );
    }

    public static function productCodeMismatch(): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.execution.product_code_mismatch'),
            errorCode: 'execution_product_code_mismatch',
        );
    }

    public static function toUpdateVersionNotGreater(int $current, mixed $target): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.execution.to_update_version_not_greater', [
                'current' => (string) $current,
                'target' => (string) ($target ?? 'null'),
            ]),
            errorCode: 'execution_to_update_version_not_greater',
        );
    }

    public static function activeExecutionExists(string $executionNo): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.execution.active_execution_exists', [
                'execution_no' => $executionNo,
            ]),
            errorCode: 'execution_active_exists',
        );
    }

    public static function invalidTransition(string $fromStatus, string $toStatus): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.execution.invalid_transition', [
                'from' => $fromStatus,
                'to' => $toStatus,
            ]),
            errorCode: 'execution_invalid_transition',
        );
    }

    public static function noVerifiedPackage(): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update.execution.no_verified_package'),
            errorCode: 'execution_no_verified_package',
        );
    }

    public static function entitlementBlocked(string $reason): self
    {
        return new self(
            userFacingMessage: $reason,
            errorCode: 'execution_entitlement_blocked',
        );
    }

    public static function invalidStartStatus(string $status): self
    {
        return new self(
            userFacingMessage: (string) __('dberp.system_update_center.errors.invalid_start_status', [
                'status' => $status,
            ]),
            errorCode: 'execution_invalid_start_status',
        );
    }

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