// This script loads local development repository settings from environment variables
// or a local.properties file, giving precedence to environment variables.

// Helper function to add the local dev Maven repository if it exists
rootProject.ext.addLocalDevRepo = { repositories ->
    if (rootProject.hasProperty('localDevReleaseRepo') && rootProject.ext.localDevReleaseRepo) {
        repositories.maven {
            url rootProject.ext.localDevReleaseRepo
            name "Local Dev Release Repo"
        }

        println "======================================"
        println "LOCAL DEV REPO ADDED"
        println("Path: $rootProject.ext.localDevReleaseRepo")
        println "======================================\n"
    }
}

rootProject.ext.addLocalDevDependency = { dependencies ->
    if (rootProject.hasProperty('localDevReleaseRepoVersion') && rootProject.ext.localDevReleaseRepoVersion) {
        def localDevDependency = "com.olo.olopay:olo-pay-android-sdk-internal:${rootProject.ext.localDevReleaseRepoVersion}"
        dependencies.implementation "$localDevDependency"

        println "======================================"
        println "LOCAL DEV DEPENDENCY ADDED"
        println("Dependency: $localDevDependency")
        println "======================================\n"
        return true
    }
    return false
}

def localRepoUrl = System.getenv('LOCAL_DEV_RELEASE_REPO')
def localRepoVersion = System.getenv('LOCAL_DEV_RELEASE_REPO_VERSION')

// Check for a local.properties file for any values not set by environment variables
//def localPropertiesFile = new File(buildscript.sourceFile.parentFile, 'local.properties')
def localPropertiesFile = new File(rootProject.projectDir, 'local.properties')
if (localPropertiesFile.exists()) {
    def properties = new Properties()
    localPropertiesFile.withInputStream { stream ->
        properties.load(stream)
    }
    // If the env var was null, try to get the value from local.properties
    if (localRepoUrl == null && properties.containsKey('localDevReleaseRepo')) {
        localRepoUrl = properties['localDevReleaseRepo']
    }
    if (localRepoVersion == null && properties.containsKey('localDevReleaseRepoVersion')) {
        localRepoVersion = properties['localDevReleaseRepoVersion']
    }

    // Set node path if it exists in local.properties
    if (properties.containsKey('node.dir')) {
        rootProject.ext.nodeDir = properties['node.dir']
    }

}

// Proceed only if both a URL and a version are provided.
if (localRepoUrl && localRepoVersion) {
    boolean isHttpRepo = localRepoUrl.startsWith('http')
    boolean isExistingLocalRepo = false

    if (!isHttpRepo) {
        def repoDir = new File(localRepoUrl)
        if (repoDir.exists() && repoDir.isDirectory()) {
            isExistingLocalRepo = true
        } else {
            println "======================================"
            println "WARNING: FALLING BACK TO PRODUCTION DEPENDENCY\n"
            println "The specified path does not exist or is not a directory:"
            println "${repoDir.absolutePath}"
            println "======================================\n"
        }
    }

    // The repo is considered valid if it's a URL or an existing local directory.
    if (isHttpRepo || isExistingLocalRepo) {
        rootProject.ext.localDevReleaseRepo = localRepoUrl
        rootProject.ext.localDevReleaseRepoVersion = localRepoVersion
        println "======================================"
        println "LOCAL DEV REPO CONFIGURED"
        println "Path: ${rootProject.ext.localDevReleaseRepo}"
        println "Version: ${rootProject.ext.localDevReleaseRepoVersion}"
        println "======================================\n"
    }
} else if ((localRepoUrl && !localRepoVersion) || (!localRepoUrl && localRepoVersion)) {
    // This case handles when a repo is provided but a version is not.
    println "======================================"
    println "WARNING: FALLING BACK TO PRODUCTION DEPENDENCY\n"
    println "Ensure both a valid repo path and version are set"
    println "by environment variables or a local.properties file\n"
    println "Environment Variables:"
    println "LOCAL_DEV_RELEASE_REPO"
    println "LOCAL_DEV_RELEASE_REPO_VERSION\n"
    println "local.properties Variables:"
    println "localDevReleaseRepo"
    println "localDevReleaseRepoVersion"
    println "======================================"
}