fun generateUniqueDescriptionsForBatch()

in core/package/src/main/kotlin/io/klibs/core/pckg/service/PackageDescriptionBatchService.kt [26:66]


    fun generateUniqueDescriptionsForBatch(
        description: String,
        packages: List<PackageEntity>
    ): Map<Long, String> {
        val batchDescriptions = mutableMapOf<Long, String>()

        val packageGroups = packages.groupBy { "${it.groupId}:${it.artifactId}" }.filterValues { it.isNotEmpty() }

        for ((_, packagesInGroup) in packageGroups) {
            val latestPackage = packagesInGroup.maxBy { it.releaseTs }

            val newDescription = packageDescriptionGenerator.generatePackageDescription(
                latestPackage.name,
                latestPackage.groupId,
                latestPackage.artifactId,
                latestPackage.version
            )

            batchDescriptions[latestPackage.idNotNull] = newDescription

            val packagesToSave = mutableListOf<PackageEntity>()

            val allVersions = packageRepository.findByGroupIdAndArtifactIdOrderByReleaseTsDesc(
                latestPackage.groupId,
                latestPackage.artifactId
            ).filter { it.description == description }

            for (pkg in allVersions) {
                val updatedPackage = pkg.deepCopy(
                    description = newDescription,
                    generatedDescription = true
                )
                packagesToSave.add(updatedPackage)
                batchDescriptions[pkg.idNotNull] = newDescription
            }

            packageRepository.saveAll(packagesToSave)
        }

        return batchDescriptions
    }