/*
Need to create backends for s3, pinpoint, predictions, core
 */
def module_backends = [
        'aws-datastore'         : 'DataStoreIntegTests',
        'aws-api'               : 'ApiInstrumentedTests',
        'aws-geo-location'      : 'GeoIntegTests',
        'maplibre-adapter'      : 'GeoIntegTests',
        'aws-storage-s3'        : 'NONE',
        'aws-analytics-pinpoint': 'NONE',
        'aws-predictions'       : 'NONE',
        'core'                  : 'NONE',
        'aws-auth-cognito'      : 'AuthIntegrationTests',
        'aws-sdk-appsync-events': 'NONE'
]

def module_subpaths = [
        'aws-sdk-appsync-events': 'appsync/'
]

def canaryTests = [
        'aws-datastore'   : ['com.amplifyframework.datastore.DatastoreCanaryTest', 'com.amplifyframework.datastore.DatastoreCanaryTestGen2'],
        'aws-auth-cognito': ['com.amplifyframework.auth.cognito.AuthCanaryTest', 'com.amplifyframework.auth.cognito.AuthCanaryTestGen2'],
        'aws-analytics-pinpoint': ['com.amplifyframework.analytics.pinpoint.PinpointAnalyticsCanaryTest'],
        'aws-geo-location': ['com.amplifyframework.geo.location.GeoCanaryTest', 'com.amplifyframework.geo.location.GeoCanaryTestGen2'],
        'aws-predictions': ['com.amplifyframework.predictions.aws.PredictionsCanaryTest'],
//      'aws-push-notifications-pinpoint': ['com.amplifyframework.pushnotifications.pinpoint.NotificationsCanaryTest'],
        'aws-storage-s3': ['com.amplifyframework.storage.s3.StorageCanaryTest', 'com.amplifyframework.storage.s3.StorageCanaryTestGen2']
]

subprojects {
    afterEvaluate { project ->
        if (module_backends.containsKey(project.name)) {
            def moduleWithSubPath = module_subpaths.get(project.name) ?
                    module_subpaths.get(project.name) + project.name : project.name
            task runTestsInDeviceFarm {
                doLast {
                    exec {
                        commandLine("$rootDir.path/scripts/run_test_in_devicefarm.sh")
                        args([moduleWithSubPath])
                    }
                }
            }
            task runNightlyTestsInDeviceFarmPool {
                dependsOn(assembleAndroidTest)
                doLast {
                    exec {
                        commandLine("$rootDir.path/scripts/run_nightly_tests_in_devicefarm_pool.sh")
                        args([project.name])
                    }
                }
            }
            task runCanaryInDeviceFarm {
                dependsOn(assembleAndroidTest)
                doLast {
                    for (canary in canaryTests[project.name]) {
                        exec {
                            commandLine("$rootDir.path/scripts/run_canary_in_devicefarm.sh")
                            args(project.name, canary)
                        }
                    }
                }
            }
        }
    }
}


task pullBackendConfigFromAmplify(description: "Pulls backend configurations from Amplify project.") {
    doLast {
        for (entry in module_backends) {
            def moduleName = entry.key
            def amplifyBackendProjectName = entry.value

            println("Getting config files for $amplifyBackendProjectName")
            def amplifyPullCommand = "$rootDir.path/scripts/pull_backend_config_from_amplify"
            exec {
                workingDir(rootDir.path)
                executable(amplifyPullCommand)
                args([amplifyBackendProjectName, moduleName])
                standardOutput(System.out)
            }
        }
    }
}
