// Create variables with empty default values
ext["ossrhUsername"] = ''
ext["ossrhPassword"] = ''
ext["sonatypeStagingProfileId"] = ''
ext["signing.keyId"] = ''
ext["signing.password"] = ''
ext["signing.key"] = ''
ext["snapshot"] = ''

File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
    // Read local.properties file first if it exists
    Properties p = new Properties()
    new FileInputStream(secretPropsFile).withCloseable { is -> p.load(is) }
    p.each { name, value -> ext[name] = value }

    // Handle signing key file reference
    def signingKeyValue = ext["signing.key"]
    if (signingKeyValue != null && signingKeyValue.toString().startsWith("@/")) {
        // Handle @/path/to/file format
        def keyFilePath = signingKeyValue.toString().substring(2)
        def keyFile = project.rootProject.file(keyFilePath)
        if (keyFile.exists()) {
            ext["signing.key"] = keyFile.text
            println "Loaded signing key from file: ${keyFilePath} (${ext['signing.key'].length()} chars)"
        } else {
            println "WARNING: Signing key file not found: ${keyFilePath}"
        }
    } else if (signingKeyValue != null && !signingKeyValue.toString().contains("BEGIN PGP")) {
        // Assume it's a file path if it doesn't contain PGP key content
        def keyFile = project.rootProject.file(signingKeyValue.toString())
        if (keyFile.exists()) {
            ext["signing.key"] = keyFile.text
            println "Loaded signing key from file: ${signingKeyValue} (${ext['signing.key'].length()} chars)"
        } else {
            println "WARNING: Signing key file not found: ${signingKeyValue}"
        }
    } else if (signingKeyValue != null) {
        println "Using signing key from properties (${signingKeyValue.length()} chars)"
    }
} else {
    // Use system environment variables
    ext["ossrhUsername"] = System.getenv('OSSRH_USERNAME')
    ext["ossrhPassword"] = System.getenv('OSSRH_PASSWORD')
    ext["sonatypeStagingProfileId"] = System.getenv('SONATYPE_STAGING_PROFILE_ID')
    ext["signing.keyId"] = System.getenv('SIGNING_KEY_ID')
    ext["signing.password"] = System.getenv('SIGNING_PASSWORD')
    ext["signing.key"] = System.getenv('SIGNING_KEY')
    ext["snapshot"] = System.getenv('SNAPSHOT')
}

if (snapshot) {
    ext["rootVersionName"] = '1.0.0'
} else {
    ext["rootVersionName"] = '1.0.0'
}

// Set up Sonatype repository
nexusPublishing {
    repositories {
        sonatype {
            stagingProfileId = sonatypeStagingProfileId
            username = ossrhUsername
            password = ossrhPassword
            version = rootVersionName

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