<?php

declare(strict_types=1);

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