plugins {
    id("java")
    id("org.jetbrains.intellij") version "1.17.4"
    id("maven-publish")
    id("signing") // נדרש רק אם מפרסמים ל-Maven Central
}

group = "co.verisoft"
version = "2024.1.4"

repositories {
    mavenCentral()
}

// 🔹 רשימת גרסאות IntelliJ שנתמכות
val supportedVersions = listOf("2023.1", "2023.2", "2024.1")

// ✅ ברירת מחדל - הגדרת הגרסה הראשית של IntelliJ (נשתמש בגרסה הכי עדכנית בטווח)
intellij {
    version.set(supportedVersions.last()) // ברירת מחדל - הגרסה האחרונה ברשימה
    updateSinceUntilBuild.set(false) // כדי שהתוסף לא יהיה תלוי בגרסה מסוימת של IntelliJ
}

// 🔹 יצירת משימות דינמיות לכל גרסה
supportedVersions.forEach { version ->
    tasks.register("buildForIntelliJ$version") {
        group = "build"
        doLast {
            println("🔨 Building plugin for IntelliJ version $version")
            intellij.version.set(version)
        }
    }
}

// 🔹 יצירת משימה אחת שתבנה לכל הגרסאות
tasks.register("buildForAllVersions") {
    group = "build"
    dependsOn(supportedVersions.map { version -> "buildForIntelliJ$version" })
    doLast {
        println("✅ Successfully built for all IntelliJ versions: ${supportedVersions.joinToString(", ")}")
    }
}

// 🔹 הגדרת פרסום ל-Maven
publishing {
    publications {
        create<MavenPublication>("mavenJava") {
            from(components["java"])

            pom {
                name.set("Verisoft IntelliJ Plugin")
                description.set("A custom IntelliJ plugin for development automation")
                url.set("https://github.com/verisoft/my-intellij-plugin")

                licenses {
                    license {
                        name.set("Apache-2.0 License")
                        url.set("https://www.apache.org/licenses/LICENSE-2.0.html")
                    }
                }

                developers {
                    developer {
                        id.set("verisoft")
                        name.set("Verisoft Team")
                    }
                }

                scm {
                    connection.set("scm:git:git://github.com/verisoft/my-intellij-plugin.git")
                    developerConnection.set("scm:git:ssh://github.com/verisoft/my-intellij-plugin.git")
                    url.set("https://github.com/verisoft/my-intellij-plugin")
                }
            }
        }
    }

    repositories {
        maven {
            name = "mavenCentral"
            url = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")

            credentials {
                username = System.getenv("OSSRH_USERNAME")
                password = System.getenv("OSSRH_PASSWORD")
            }
        }
    }
}

// 🔹 חתימה אם מפרסמים ל-Maven Central
signing {
    sign(publishing.publications["mavenJava"])
}
