apply plugin: 'maven-publish'
apply plugin: 'signing'

def isReleaseBuild() {
    return !VERSION_NAME.contains("-SNAPSHOT")
}



android {
    publishing {
        singleVariant("release") {
            // if you don't want sources/javadoc, remove these lines
            withJavadocJar()
        }
    }
}

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                // The coordinates of the library
                groupId = GROUP
                artifactId = POM_ARTIFACT_ID
                version = VERSION_NAME

                // Artifacts to publish
                artifact("$buildDir/outputs/aar/${project.name}-release.aar")

                // Configure POM metadata
                pom {
                    name.set(POM_NAME)
                    description.set(POM_DESCRIPTION)
                    url.set(POM_URL)

                    licenses {
                        license {
                            name.set(POM_LICENCE_NAME)
                            url.set(POM_LICENCE_URL)
                            distribution.set(POM_LICENCE_DIST)
                        }
                    }

                    developers {
                        developer {
                            id.set(POM_DEVELOPER_ID)
                            name.set(POM_DEVELOPER_NAME)
                        }
                    }

                    scm {
                        connection.set(POM_SCM_CONNECTION)
                        developerConnection.set(POM_SCM_DEV_CONNECTION)
                        url.set(POM_SCM_URL)
                    }
                }

                // Add dependencies to the POM
                pom.withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')
                    configurations.implementation.allDependencies.each { dependency ->
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', dependency.group ?: '')
                        dependencyNode.appendNode('artifactId', dependency.name)
                        dependencyNode.appendNode('version', dependency.version ?: '')
                    }
                }
            }
        }


    }


    def decodedKey = project.findProperty("signing.secretKey")?.replace('\\n', '\n')

    signing {
        // 🧪 STEP 2: Use the decoded key INSIDE the signing block
        if (project.hasProperty("signing.keyId") &&
                decodedKey &&
                project.hasProperty("signing.password")) {

            useInMemoryPgpKeys(
                    project.findProperty("signing.keyId")?.toString(),
                    decodedKey,
                    project.findProperty("signing.password")?.toString()
            )
        }

        sign publishing.publications.release
    }
//    task androidJavadocs(type: Javadoc) {
//        classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
//        if (JavaVersion.current().isJava8Compatible()) {
//                tasks.withType(Javadoc) { options.addStringOption('Xdoclint:none', '-quiet') }
//            }
//        }
//    }
//    task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
//        archiveClassifier = 'javadoc'
//        from androidJavadocs.destinationDir
//    }
//    task androidSourcesJar(type: Jar) {
//        classifier = 'sources'
//        from android.sourceSets.main.java.sourceFiles
//    }
//    artifacts { archives androidJavadocsJar }
}