plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("org.jetbrains.kotlin.plugin.serialization")
    id("org.jetbrains.kotlin.plugin.compose")
}

fun propOrEnv(name: String, defaultValue: String): String {
    val gradleValue = project.findProperty(name) as String?
    val envValue = System.getenv(name)
    return when {
        !gradleValue.isNullOrBlank() -> gradleValue
        !envValue.isNullOrBlank() -> envValue
        else -> defaultValue
    }
}

android {
    namespace = "de.trainingstagebuch.app"
    compileSdk = 34

    defaultConfig {
        applicationId = "de.trainingstagebuch.app"
        minSdk = 26
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix = ".debug"
            versionNameSuffix = "-debug"
            resValue("string", "app_name", "Trainingstagebuch Debug")
            buildConfigField(
                "String",
                "API_BASE_URL",
                "\"${propOrEnv("API_BASE_URL_DEBUG", "http://10.0.2.2:3005/api/")}\""
            )
            buildConfigField(
                "String",
                "SOCKET_BASE_URL",
                "\"${propOrEnv("SOCKET_BASE_URL_DEBUG", "http://10.0.2.2:3005")}\""
            )
        }
        release {
            isMinifyEnabled = false
            resValue("string", "app_name", "Trainingstagebuch")
            buildConfigField(
                "String",
                "API_BASE_URL",
                "\"${propOrEnv("API_BASE_URL_RELEASE", "http://10.0.2.2:3005/api/")}\""
            )
            buildConfigField(
                "String",
                "SOCKET_BASE_URL",
                "\"${propOrEnv("SOCKET_BASE_URL_RELEASE", "http://10.0.2.2:3005")}\""
            )
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }

    buildFeatures {
        compose = true
        buildConfig = true
    }

    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

tasks.register<Exec>("checkI18nHardcodedUiStrings") {
    workingDir = rootProject.projectDir
    commandLine("bash", "scripts/check_no_hardcoded_ui_strings.sh")
}

tasks.named("preBuild").configure {
    dependsOn("checkI18nHardcodedUiStrings")
}

dependencies {
    val composeBom = platform("androidx.compose:compose-bom:2024.10.01")

    implementation("androidx.core:core-ktx:1.13.1")
    implementation("androidx.appcompat:appcompat:1.7.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7")
    implementation("androidx.activity:activity-compose:1.9.3")
    implementation(composeBom)
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")

    implementation("androidx.navigation:navigation-compose:2.8.3")
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.7")
    implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.7")

    implementation("androidx.datastore:datastore-preferences:1.1.1")

    implementation("com.squareup.retrofit2:retrofit:2.11.0")
    implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:1.0.0")
    implementation("com.squareup.okhttp3:okhttp:4.12.0")
    implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")

    testImplementation("junit:junit:4.13.2")

    debugImplementation(composeBom)
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")
}
