import java.util.Properties

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.compose)
}

val releaseSigningPropertiesFile =
    rootProject.file("release-signing.properties")

val releaseSigningProperties =
    Properties().apply {
        if (releaseSigningPropertiesFile.exists()) {
            releaseSigningPropertiesFile
                .reader(Charsets.UTF_8)
                .use { load(it) }
        }
    }

android {
    namespace = "ru.durakscore.app"

    compileSdk {
        version = release(37)
    }

    defaultConfig {
        applicationId = "ru.durakscore.app"

        minSdk = 26
        targetSdk = 37

        versionCode = 30
        versionName = "0.9.20"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
        create("release") {
            if (releaseSigningPropertiesFile.exists()) {
                storeFile =
                    file(
                        releaseSigningProperties.getProperty(
                            "storeFile"
                        )
                    )

                storePassword =
                    releaseSigningProperties.getProperty(
                        "storePassword"
                    )

                keyAlias =
                    releaseSigningProperties.getProperty(
                        "keyAlias"
                    )

                keyPassword =
                    releaseSigningProperties.getProperty(
                        "keyPassword"
                    )
            }
        }
    }

    buildTypes {
        release {
            if (releaseSigningPropertiesFile.exists()) {
                signingConfig =
                    signingConfigs.getByName(
                        "release"
                    )
            }

            optimization {
                enable = false
            }
        }
    }

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

    buildFeatures {
        compose = true
    }
}

val releaseVersionName =
    android.defaultConfig.versionName
        ?: error("versionName is not set")

val releaseVersionCode =
    android.defaultConfig.versionCode

tasks.register<Exec>("publishRelease") {
    notCompatibleWithConfigurationCache("External PowerShell SSH release task")
    group = "release"
    description =
        "Build signed APK and publish Durakometr update to VPS"

    dependsOn("assembleRelease")

    doFirst {
        check(
            releaseSigningPropertiesFile.exists()
        ) {
            "Missing release-signing.properties in project root"
        }

        val requiredKeys =
            listOf(
                "storeFile",
                "storePassword",
                "keyAlias",
                "keyPassword"
            )

        requiredKeys.forEach { key ->
            check(
                !releaseSigningProperties
                    .getProperty(key)
                    .isNullOrBlank()
            ) {
                "Missing '$key' in release-signing.properties"
            }
        }
    }

    workingDir =
        rootProject.projectDir

    commandLine(
        "powershell.exe",
        "-NoProfile",
        "-ExecutionPolicy",
        "Bypass",
        "-File",
        rootProject
            .file("publish-release.ps1")
            .absolutePath,
        "-VersionName",
        releaseVersionName,
        "-VersionCode",
        releaseVersionCode.toString()
    )
}

dependencies {

    implementation(platform(libs.androidx.compose.bom))

    implementation("androidx.appcompat:appcompat:1.7.1")

    implementation(libs.androidx.activity.compose)
    implementation(libs.androidx.compose.material3)
    implementation(libs.androidx.compose.ui)
    implementation(libs.androidx.compose.ui.graphics)
    implementation(libs.androidx.compose.ui.tooling.preview)

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)

    testImplementation(libs.junit)

    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.compose.ui.test.junit4)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(libs.androidx.junit)

    debugImplementation(libs.androidx.compose.ui.test.manifest)
    debugImplementation(libs.androidx.compose.ui.tooling)
}
