package org.gradle.integtests.resolve.catalog

import org.gradle.integtests.resolve.PluginDslSupport
import org.gradle.test.fixtures.file.LeaksFileHandles
import org.gradle.test.fixtures.plugin.PluginBuilder
import org.gradle.test.fixtures.server.http.MavenHttpPluginRepository
import org.junit.Rule

@LeaksFileHandles("Kotlin Compiler Daemon working directory")
class DefaultVersionCatalogIntegrationTest extends AbstractVersionCatalogIntegrationTest implements PluginDslSupport {
    @Rule
    final MavenHttpPluginRepository pluginPortal = MavenHttpPluginRepository.asGradlePluginPortal(executer, mavenRepo)

    def "can apply a plugin declared in a catalog"() {
        String taskName = 'greet'
        String message = 'Hello from plugin!'
        String pluginId = 'com.acme.greeter'
        String pluginVersion = '1.5'
        String filePath = 'gradle/libs.versions.toml'

        versionCatalogFile << "[versions]\n" +
            "swaggerCoreVersion = \"2.2.25\"\n" +
            "springDocVersion = \"1.8.0\"\n" +
            "classGraphVersion = \"4.8.112\"\n" +
            "\n" +
            "[libraries]\n" +
            "swaggercore = {group=\"io.swagger.core.v3\", name=\"swagger-core\", version.ref = \"swaggerCoreVersion\"}\n" +
            "swaggerannotations = {group=\"io.swagger.core.v3\", name=\"swagger-annotations\", version.ref = \"swaggerCoreVersion\"}\n" +
            "swaggerintegration = {group=\"io.swagger.core.v3\", name=\"swagger-integration\", version.ref = \"swaggerCoreVersion\"}\n" +
            "classgraph = {group=\"io.github.classgraph\", name=\"classgraph\", version.ref = \"classGraphVersion\"}\n" +
            "springdoc = {group=\"org.springdoc\", name=\"springdoc-openapi-ui\", version.ref = \"springDocVersion\"}\n"


        def file = new File(filePath)

        file.text ="[versions]\n" +
            "swaggerCoreVersion = \"2.2.25\"\n" +
            "springDocVersion = \"1.8.0\"\n" +
            "classGraphVersion = \"4.8.112\"\n" +
            "\n" +
            "[libraries]\n" +
            "swaggercore = {group=\"io.swagger.core.v3\", name=\"swagger-core\", version.ref = \"swaggerCoreVersion\"}\n" +
            "swaggerannotations = {group=\"io.swagger.core.v3\", name=\"swagger-annotations\", version.ref = \"swaggerCoreVersion\"}\n" +
            "swaggerintegration = {group=\"io.swagger.core.v3\", name=\"swagger-integration\", version.ref = \"swaggerCoreVersion\"}\n" +
            "classgraph = {group=\"io.github.classgraph\", name=\"classgraph\", version.ref = \"classGraphVersion\"}\n" +
            "springdoc = {group=\"org.springdoc\", name=\"springdoc-openapi-ui\", version.ref = \"springDocVersion\"}\n"

        // We use the Groovy DSL for settings because that's not what we want to
        // test and the setup would be more complicated with Kotlin
        settingsKotlinFile << """
rootProject.name = "learn"

dependencyResolutionManagement {
    versionCatalogs {
        create("libs") {
            from(files("gradle/libs.versions.toml"))
        }
    }
}"""
        buildKotlinFile << """
plugins {
\tjava
\twar
\tid("org.springframework.boot") version "3.3.5"
\tid("io.spring.dependency-management") version "1.1.6"
}

group = "com.amal"
version = "0.0.1-SNAPSHOT"

java {
\ttoolchain {
\t\tlanguageVersion = JavaLanguageVersion.of(17)
\t}
}

configurations {
\tcompileOnly {
\t\textendsFrom(configurations.annotationProcessor.get())
\t}
}

repositories {
\tmavenCentral()
}

dependencies {
\timplementation("org.springframework.boot:spring-boot-starter-oauth2-authorization-server")
\timplementation("org.springframework.boot:spring-boot-starter-web")
\timplementation(libs.swaggercore)
\timplementation(libs.swaggerannotations)
\timplementation(libs.swaggerintegration)
\timplementation(libs.classgraph)
\timplementation(libs.springdoc)
\tcompileOnly("org.projectlombok:lombok")
\tdevelopmentOnly("org.springframework.boot:spring-boot-devtools")
\truntimeOnly("org.postgresql:postgresql")
\tannotationProcessor("org.projectlombok:lombok")
\tprovidedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
\ttestImplementation("org.springframework.boot:spring-boot-starter-test")
\ttestRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

tasks.withType<Test> {
\tuseJUnitPlatform()
}

"""

        when:
        fails "build"

        then:
        outputContains message
    }
}