<?php

declare(strict_types=1);

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

namespace App\Services\SupportCenter;

/**
 * 从已解析 manifest 提取可持久化的安全摘要（不含密钥与完整 checksum 映射）。
 */
final class HostSystemUpdateExecutionManifestSummarizer
{
    /**
     * @param  array<string, mixed>  $manifest
     * @return array<string, mixed>
     */
    public function summarize(array $manifest): array
    {
        $summary = [
            HostSystemUpdateManifestContract::PRODUCT_CODE => $this->nullableString($manifest[HostSystemUpdateManifestContract::PRODUCT_CODE] ?? null),
            HostSystemUpdateManifestContract::FROM_VERSION => $this->nullableString($manifest[HostSystemUpdateManifestContract::FROM_VERSION] ?? null),
            HostSystemUpdateManifestContract::FROM_UPDATE_VERSION => $this->nullableInt($manifest[HostSystemUpdateManifestContract::FROM_UPDATE_VERSION] ?? null),
            HostSystemUpdateManifestContract::TO_VERSION => $this->nullableString($manifest[HostSystemUpdateManifestContract::TO_VERSION] ?? null),
            HostSystemUpdateManifestContract::TO_UPDATE_VERSION => $this->nullableInt($manifest[HostSystemUpdateManifestContract::TO_UPDATE_VERSION] ?? null),
            HostSystemUpdateManifestContract::PACKAGE_TYPE => $this->nullableString($manifest[HostSystemUpdateManifestContract::PACKAGE_TYPE] ?? null),
            HostSystemUpdateManifestContract::RELEASE_CHANNEL => $this->nullableString($manifest[HostSystemUpdateManifestContract::RELEASE_CHANNEL] ?? null),
            HostSystemUpdateManifestContract::BACKUP_REQUIRED => $manifest[HostSystemUpdateManifestContract::BACKUP_REQUIRED] ?? null,
            HostSystemUpdateManifestContract::MAINTENANCE_MODE_REQUIRED => $manifest[HostSystemUpdateManifestContract::MAINTENANCE_MODE_REQUIRED] ?? null,
            HostSystemUpdateManifestContract::ROLLBACK_STRATEGY => $this->nullableString($manifest[HostSystemUpdateManifestContract::ROLLBACK_STRATEGY] ?? null),
            'files_count' => $this->countList($manifest[HostSystemUpdateManifestContract::FILES] ?? null),
            'delete_files_count' => $this->countList($manifest[HostSystemUpdateManifestContract::DELETE_FILES] ?? null),
            'migrations_count' => $this->countList($manifest[HostSystemUpdateManifestContract::MIGRATIONS] ?? null),
            'commands_count' => $this->countList($manifest[HostSystemUpdateManifestContract::COMMANDS] ?? null),
            'checksums_count' => $this->countMap($manifest[HostSystemUpdateManifestContract::CHECKSUMS] ?? null),
        ];

        return SupportCenterSnapshotSanitizer::sanitize($summary);
    }

    private function countList(mixed $value): int
    {
        return is_array($value) ? count($value) : 0;
    }

    private function countMap(mixed $value): int
    {
        return is_array($value) ? count($value) : 0;
    }

    private function nullableString(mixed $value): ?string
    {
        if (! is_string($value)) {
            return null;
        }

        $trimmed = trim($value);

        return $trimmed === '' ? null : $trimmed;
    }

    private function nullableInt(mixed $value): ?int
    {
        if ($value === null || $value === '') {
            return null;
        }

        if (is_int($value)) {
            return $value;
        }

        if (is_numeric($value)) {
            return (int) $value;
        }

        return null;
    }
}
