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

// ── Git-based versioning ────────────────────────────────────
fun runGit(vararg args: String): String = try {
    val proc = ProcessBuilder("git", *args)
        .directory(rootProject.projectDir)
        .redirectErrorStream(true)
        .start()
    proc.inputStream.bufferedReader().readText().trim().also { proc.waitFor() }
} catch (_: Exception) { "" }

// Version name from latest git tag (e.g. v0.1.0 -> "0.1.0")
fun gitVersionName(): String = runGit("describe", "--tags", "--abbrev=0")
    .removePrefix("v")
    .ifEmpty { "0.1.0" }

// Build number from .buildcount file (incremented by deploy.ps1)
fun buildNumber(): Int {
    val file = rootProject.file(".buildcount")
    return if (file.exists()) file.readText().trim().toIntOrNull() ?: 1 else 1
}

fun gitShortHash(): String = runGit("rev-parse", "--short", "HEAD")
    .ifEmpty { "unknown" }

// Display: "0.1.0+7.abc1234"
fun gitDisplayVersion(): String = "${gitVersionName()}+${buildNumber()}.${gitShortHash()}"

// ─────────────────────────────────────────────────────────────

android {
    namespace = "com.omixlab.lckcontrol"
    compileSdk {
        version = release(36) {
            minorApiLevel = 1
        }
    }

    signingConfigs {
        create("release") {
            storeFile = rootProject.file("lck-control.keystore")
            storePassword = "4gx%wx4NOhS6"
            keyAlias = "lck-control"
            keyPassword = "4gx%wx4NOhS6"
        }
    }

    defaultConfig {
        applicationId = "com.omixlab.lckcontrol"
        minSdk = 32
        targetSdk = 34
        versionCode = buildNumber()
        versionName = gitVersionName()

        buildConfigField("String", "DISPLAY_VERSION", "\"${gitDisplayVersion()}\"")

        ndk {
            abiFilters += listOf("arm64-v8a")
        }

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

    externalNativeBuild {
        cmake {
            path = file("src/main/cpp/CMakeLists.txt")
            version = "3.22.1"
        }
    }

    buildTypes {
        debug {
            signingConfig = signingConfigs.getByName("release")
        }
        release {
            signingConfig = signingConfigs.getByName("release")
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }
    buildFeatures {
        compose = true
        buildConfig = true
    }
    packaging {
        resources {
            excludes += setOf(
                "META-INF/DEPENDENCIES",
                "META-INF/LICENSE",
                "META-INF/LICENSE.txt",
                "META-INF/NOTICE",
                "META-INF/NOTICE.txt",
            )
        }
    }
}

dependencies {
    implementation(project(":shared"))

    // Core
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.lifecycle.viewmodel.compose)
    implementation(libs.androidx.lifecycle.runtime.compose)
    implementation(libs.kotlinx.coroutines.android)

    // Compose
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.compose.ui)
    implementation(libs.androidx.compose.ui.graphics)
    implementation(libs.androidx.compose.ui.tooling.preview)
    implementation(libs.androidx.compose.material3)
    implementation(libs.androidx.compose.material.icons.extended)
    implementation(libs.androidx.navigation.compose)

    // Hilt
    implementation(libs.hilt.android)
    ksp(libs.hilt.compiler)
    implementation(libs.androidx.hilt.navigation.compose)

    // Room
    implementation(libs.room.runtime)
    implementation(libs.room.ktx)
    ksp(libs.room.compiler)

    // Networking
    implementation(libs.retrofit)
    implementation(libs.retrofit.converter.moshi)
    implementation(libs.moshi.kotlin)
    implementation(libs.okhttp)
    implementation(libs.okhttp.logging)

    // Security
    implementation(libs.androidx.security.crypto)

    // Meta Quest Platform SDK
    implementation("com.meta.horizon.platform.ovr:android-platform-sdk:77.0.1")

    // Browser (Custom Tabs for OAuth flows)
    implementation(libs.androidx.browser)

    // Test
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.compose.ui.test.junit4)
    debugImplementation(libs.androidx.compose.ui.tooling)
    debugImplementation(libs.androidx.compose.ui.test.manifest)
}
