/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.internal.configuration.problems

import org.gradle.api.internal.DocumentationRegistry
import org.gradle.internal.cc.impl.problems.JsonSource
import org.gradle.internal.cc.impl.problems.JsonWriter


data class ProblemReportDetails(
    val buildDisplayName: String?,
    val cacheAction: String,
    val cacheActionDescription: StructuredMessage,
    val requestedTasks: String?,
    val totalProblemCount: Int,
    val uniqueProblemCount: Int,
    val overflownProblemCount: Int
)


class ProblemReportDetailsJsonSource(private val details: ProblemReportDetails) : JsonSource {
    override fun writeToJson(jsonWriter: JsonWriter) {
        with(jsonWriter) {
            property("totalProblemCount", details.totalProblemCount)
            property("uniqueProblemCount", details.uniqueProblemCount)
            property("overflownProblemCount", details.overflownProblemCount)
            details.buildDisplayName?.let { property("buildName", it) }
            details.requestedTasks?.let {
                property("requestedTasks", it)
            }
            property("cacheAction", details.cacheAction)
            property("cacheActionDescription") {
                writeStructuredMessage(details.cacheActionDescription)
            }
            property("documentationLink", DocumentationRegistry().getDocumentationFor("configuration_cache"))
        }
    }
}
