import java.util.Properties
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

val backendBaseUrlForRelease = providers.gradleProperty("backendBaseUrl")
    .orElse("https://tt-tagebuch.de")
    .get()

val backendBaseUrlForDebug = providers.gradleProperty("backendBaseUrl")
    .orElse("http://10.0.2.2:3005")
    .get()

val socketBaseUrl = providers.gradleProperty("socketBaseUrl")
    .orElse("wss://tt-tagebuch.de:3051")
    .get()

val signingPropertiesFile = rootProject.file("signing.properties")
val signingProperties = Properties().apply {
    if (signingPropertiesFile.isFile) {
        signingPropertiesFile.inputStream().use(::load)
    }
}

fun signingValue(name: String, envName: String): String? =
    signingProperties.getProperty(name)?.takeIf { it.isNotBlank() }
        ?: System.getenv(envName)?.takeIf { it.isNotBlank() }

val releaseStoreFile = signingValue("storeFile", "TTT_RELEASE_STORE_FILE")
val releaseStorePassword = signingValue("storePassword", "TTT_RELEASE_STORE_PASSWORD")
val releaseKeyAlias = signingValue("keyAlias", "TTT_RELEASE_KEY_ALIAS")
val releaseKeyPassword = signingValue("keyPassword", "TTT_RELEASE_KEY_PASSWORD")

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.jetbrainsCompose)
    alias(libs.plugins.compose.compiler)
}

kotlin {
    androidTarget {
        compilations.all {
            compilerOptions.configure {
                jvmTarget.set(JvmTarget.JVM_17)
            }
        }
    }
    
    sourceSets {
        commonMain.dependencies {
            implementation(project(":shared"))
            implementation(compose.runtime)
            implementation(compose.foundation)
            implementation(compose.material)
            implementation(compose.materialIconsExtended)
            implementation(compose.ui)
            implementation(compose.components.resources)
            implementation(libs.voyager.navigator)
            implementation(libs.voyager.screenmodel)
            implementation(libs.voyager.koin)
            implementation(libs.koin.core)
        }
        androidMain.dependencies {
            implementation(libs.androidx.activity.compose)
            implementation(libs.androidx.appcompat)
            implementation(libs.androidx.core.ktx)
            implementation(libs.koin.android)
            implementation(libs.coil.compose)
            implementation(libs.yalantis.ucrop)
            implementation(libs.ktor.serialization.kotlinx.json)
        }
    }
}

android {
    namespace = "de.tsschulz.tt_tagebuch"
    compileSdk = libs.versions.android.compileSdk.get().toInt()

    defaultConfig {
        applicationId = "de.tsschulz.tt_tagebuch"
        minSdk = libs.versions.android.minSdk.get().toInt()
        targetSdk = libs.versions.android.targetSdk.get().toInt()
        versionCode = libs.versions.appVersionCode.get().toInt()
        versionName = libs.versions.appVersionName.get()
        // BUILD CONFIGS are provided per-buildType below. SOCKET_BASE_URL remains in defaultConfig
        buildConfigField("String", "SOCKET_BASE_URL", "\"$socketBaseUrl\"")
    }
    buildFeatures {
        buildConfig = true
    }
    signingConfigs {
        create("releaseSigning") {
            if (releaseStoreFile != null) storeFile = file(releaseStoreFile)
            if (releaseStorePassword != null) storePassword = releaseStorePassword
            if (releaseKeyAlias != null) keyAlias = releaseKeyAlias
            if (releaseKeyPassword != null) keyPassword = releaseKeyPassword
        }
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
    buildTypes {
        getByName("release") {
            if (releaseStoreFile != null && releaseStorePassword != null && releaseKeyAlias != null && releaseKeyPassword != null) {
                signingConfig = signingConfigs.getByName("releaseSigning")
            }
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro",
            )
            // Release: default to production, can be overridden with -PbackendBaseUrl
            buildConfigField("String", "BACKEND_BASE_URL", "\"$backendBaseUrlForRelease\"")
        }
        getByName("debug") {
            // Debug: default to emulator localhost via 10.0.2.2, can be overridden with -PbackendBaseUrl
            buildConfigField("String", "BACKEND_BASE_URL", "\"$backendBaseUrlForDebug\"")
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
}

dependencies {
    debugImplementation(libs.compose.ui.tooling)
}
