import java.util.Properties

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("dev.flutter.flutter-gradle-plugin")
}

val sharedSigningPropertiesFile =
    rootProject.projectDir.parentFile.parentFile.resolve("android/key.properties")
val sharedSigningProperties =
    Properties().also { properties ->
        check(sharedSigningPropertiesFile.isFile) {
            "Shared signing config missing: ${sharedSigningPropertiesFile.absolutePath}"
        }
        sharedSigningPropertiesFile.inputStream().use(properties::load)
    }

fun requiredSharedSigningProperty(name: String): String =
    sharedSigningProperties.getProperty(name)?.takeIf { it.isNotBlank() }
        ?: error("Shared signing property '$name' missing in ${sharedSigningPropertiesFile.absolutePath}")

val sharedSigningStoreFile =
    sharedSigningPropertiesFile.parentFile.resolve(requiredSharedSigningProperty("storeFile"))

check(sharedSigningStoreFile.isFile) {
    "Shared signing keystore missing: ${sharedSigningStoreFile.absolutePath}"
}

android {
    namespace = "com.gametime.watch"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    defaultConfig {
        // Wear OS Data Layer requires matching package names and signatures
        // across phone and watch APKs.
        applicationId = "com.gametime.app"
        minSdk = 30
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName
    }

    signingConfigs {
        create("sharedLocal") {
            storeFile = sharedSigningStoreFile
            storePassword = requiredSharedSigningProperty("storePassword")
            keyAlias = requiredSharedSigningProperty("keyAlias")
            keyPassword = requiredSharedSigningProperty("keyPassword")
        }
    }

    buildTypes {
        release {
            signingConfig = signingConfigs.getByName("sharedLocal")
        }
    }
}

kotlin {
    compilerOptions {
        jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17
    }
}

flutter {
    source = "../.."
}

dependencies {
    implementation("androidx.core:core-ktx:1.13.1")
    implementation("androidx.health:health-services-client:1.1.0-rc02")
    implementation("androidx.wear:wear:1.4.0")
    implementation("androidx.wear:wear-ongoing:1.0.0")
    implementation("com.google.guava:guava:33.6.0-android")
    implementation("com.google.android.gms:play-services-wearable:19.0.0")

    testImplementation(kotlin("test"))
}
