plugins {
    id 'org.jetbrains.kotlin.jvm' version '2.3.21'
    id "org.jetbrains.dokka-javadoc" version "2.2.0"
    id 'maven-publish'
    id 'signing'
    id 'io.github.gradle-nexus.publish-plugin' version '2.0.0'
    id 'com.palantir.git-version' version '4.3.0'
    id 'jacoco'
}

apply from: 'hooks.gradle'

group = 'io.github.lsd-consulting'
version = gitVersion().replaceAll("^v", "")
logger.lifecycle("Version:$version")

repositories {
    mavenCentral()
    mavenLocal()
}

kotlin {
    jvmToolchain(17)
}

dependencies {
    implementation 'com.github.jknack:handlebars:4.5.1'
    implementation 'net.sourceforge.plantuml:plantuml-mit:1.2025.10'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.11.0'

    testImplementation 'org.assertj:assertj-core:3.27.7'
    testImplementation 'com.approvaltests:approvaltests:26.7.1'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:6.0.3'
    testImplementation 'org.junit.jupiter:junit-jupiter-params:6.0.3'
    testImplementation 'org.junit-pioneer:junit-pioneer:2.3.0'
    testImplementation "org.mockito.kotlin:mockito-kotlin:6.3.0"
    testImplementation 'com.microsoft.playwright:playwright:1.59.0'

    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:6.0.3'
}

jacocoTestReport {
    reports {
        xml.required = true
        html.required = true
        html.outputLocation = layout.buildDirectory.dir("reports/coverage")
    }
}

test {
    useJUnitPlatform()
    testLogging {
        events "failed"
        exceptionFormat "full"
    }
    finalizedBy jacocoTestReport
}

java {
    withSourcesJar()
    withJavadocJar()
}

// Configure the javadoc task to use Dokka
javadocJar {
    dependsOn dokkaGeneratePublicationJavadoc
    from layout.buildDirectory.dir('dokka/javadoc')
}

artifacts {
    archives jar
    archives sourcesJar
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId = "$group"
            artifactId = 'lsd-core'
            version = "${version}"

            from components.kotlin
            artifact sourcesJar
            artifact javadocJar
            pom {
                name = 'lsd-core'
                description = 'A library used to generate living sequence diagrams'
                url = 'https://github.com/lsd-consulting/lsd-core.git'
                licenses {
                    license {
                        name = "MIT License"
                        url = "https://github.com/lsd-consulting/lsd-core/blob/main/LICENSE.txt"
                        distribution = "repo"
                    }
                }
                developers {
                    developer {
                        name = "Nick"
                        email = "nicholas.mcdowall@gmail.com"
                        organization = 'NKM IT Solutions'
                        organizationUrl = 'https://github.com/nickmcdowall'
                    }
                    developer {
                        name = "Lukasz"
                        email = "lukasz.gryzbon@gmail.com"
                        organization = 'Integreety Ltd.'
                        organizationUrl = 'https://github.com/integreety'
                    }
                }
                scm {
                    url = "https://github.com/lsd-consulting/lsd-core.git"
                }
            }
            repositories {
                maven {
                    name = 'ossrh-staging-api'
                    url = "https://ossrh-staging-api.central.sonatype.com/service/local/staging/deploy/maven2/"
                    credentials(PasswordCredentials)
                }
            }
        }
    }
}

nexusPublishing {
    repositories {
        sonatype {
            nexusUrl.set(uri("https://ossrh-staging-api.central.sonatype.com/service/local/"))
            snapshotRepositoryUrl.set(uri("https://central.sonatype.com/repository/maven-snapshots/"))
        }
    }
}

signing {
    if (project.findProperty("signingKey")) {
        // Use in-memory ascii-armored keys
        def signingKey = findProperty("signingKey")
        def signingPassword = findProperty("signingPassword")
        useInMemoryPgpKeys(signingKey, signingPassword)
        sign publishing.publications.mavenJava
    } else {
        // Use signing properties in ~/.gradle/gradle.properties
        sign publishing.publications.mavenJava
    }
}
