import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.composeMultiplatform)
    alias(libs.plugins.composeCompiler)
    alias(libs.plugins.composeHotReload)
    alias(libs.plugins.kotlinxSerialization)
    alias(libs.plugins.googleServices)
    alias(libs.plugins.roborazzi)
    id("co.touchlab.skie") version "0.10.7"
}

kotlin {
    androidTarget {
        compilerOptions {
            jvmTarget.set(JvmTarget.JVM_11)
        }
    }
    
    listOf(
        iosArm64(),
        iosSimulatorArm64()
    ).forEach { iosTarget ->
        iosTarget.binaries.framework {
            baseName = "ComposeApp"
            isStatic = true
        }
    }
    
    jvm()
    
    js {
        browser()
        binaries.executable()
    }
    
    @OptIn(ExperimentalWasmDsl::class)
    wasmJs {
        browser()
        binaries.executable()
    }
    
    sourceSets {
        androidMain.dependencies {
            implementation(compose.preview)
            implementation(libs.androidx.activity.compose)
            implementation(libs.ktor.client.okhttp)

//            implementation(libs.ktor.client.logging)
            implementation(libs.kotlinx.coroutines.android)
            implementation(libs.google.billing)

            // PostHog Analytics
            implementation("com.posthog:posthog-android:3.8.2")

            // Google Sign In - Credential Manager
            implementation("androidx.credentials:credentials:1.3.0")
            implementation("androidx.credentials:credentials-play-services-auth:1.3.0")
            implementation("com.google.android.libraries.identity.googleid:googleid:1.1.1")

            // Jetpack Glance for Home Screen Widgets
            implementation("androidx.glance:glance-appwidget:1.1.1")
            implementation("androidx.glance:glance-material3:1.1.1")

            // DataStore for widget data persistence
            implementation("androidx.datastore:datastore-preferences:1.1.1")

            // WorkManager for scheduled widget refresh (iOS parity — Stream L)
            implementation("androidx.work:work-runtime-ktx:2.9.1")

            // Encrypted SharedPreferences for secure token storage
            implementation(libs.androidx.security.crypto)

            // Biometric authentication (requires FragmentActivity)
            implementation("androidx.biometric:biometric:1.1.0")
            implementation("androidx.fragment:fragment-ktx:1.8.5")

            // EXIF orientation reader for ImageCompression (P6 Stream V)
            implementation("androidx.exifinterface:exifinterface:1.3.7")
        }
        iosMain.dependencies {
            implementation(libs.ktor.client.darwin)
        }
        jvmMain.dependencies {
            implementation(compose.desktop.currentOs)
            implementation(libs.kotlinx.coroutinesSwing)
            implementation(libs.ktor.client.cio)
        }
        jsMain.dependencies {
            implementation(libs.ktor.client.js)
        }
        wasmJsMain.dependencies {
            implementation(libs.ktor.client.js)
        }
        commonMain.dependencies {
            implementation(libs.ktor.serialization.kotlinx.json)
            implementation(libs.ktor.client.content.negotiation)
            implementation(compose.runtime)
            implementation(compose.foundation)
            implementation(compose.material3)
            implementation(compose.ui)
            implementation(compose.components.resources)
            implementation(compose.components.uiToolingPreview)
            implementation(libs.androidx.lifecycle.viewmodelCompose)
            implementation(libs.androidx.lifecycle.runtimeCompose)
            implementation(libs.androidx.navigation.compose)
            implementation(libs.ktor.client.core)
            implementation(libs.kotlinx.coroutines.core)
            implementation(libs.kotlinx.datetime)
            implementation(libs.ktor.client.logging)
            implementation(libs.ktor.client.encoding)
            implementation(compose.materialIconsExtended)
            implementation(libs.coil.compose)
            implementation(libs.coil.network.ktor3)
        }
        commonTest.dependencies {
            implementation(libs.kotlin.test)
            implementation(libs.ktor.client.mock)
            implementation(libs.kotlinx.coroutines.test)
        }
        val androidUnitTest by getting {
            dependencies {
                implementation(libs.kotlin.testJunit)
                implementation(libs.junit)
                implementation(libs.robolectric)
                implementation(libs.mockk)
                implementation(libs.kotlinx.coroutines.test)
                implementation(libs.androidx.test.core)
                implementation(libs.androidx.test.core.ktx)
                implementation(libs.androidx.testExt.junit)
                implementation("androidx.work:work-testing:2.9.1")
                // Roborazzi screenshot regression tooling (P8). Runs on the
                // Robolectric-backed JVM unit-test classpath; no emulator
                // required. Add compose ui-test so the rule's composeRule
                // parameter compiles.
                implementation(libs.roborazzi)
                implementation(libs.roborazzi.compose)
                implementation(libs.roborazzi.junit.rule)
                implementation(libs.compose.ui.test.junit4.android)
                implementation(libs.compose.ui.test.manifest)
            }
        }
        val androidInstrumentedTest by getting {
            dependencies {
                implementation(libs.androidx.testExt.junit)
                implementation(libs.androidx.espresso.core)
                implementation(libs.androidx.test.runner)
                implementation(libs.mockk.android)
                implementation(libs.compose.ui.test.junit4.android)
                implementation("androidx.test.uiautomator:uiautomator:2.3.0")
            }
        }
    }
}

android {
    namespace = "com.tt.honeyDue"
    compileSdk = libs.versions.android.compileSdk.get().toInt()

    defaultConfig {
        applicationId = "com.tt.honeyDue"
        minSdk = libs.versions.android.minSdk.get().toInt()
        targetSdk = libs.versions.android.targetSdk.get().toInt()
        versionCode = 1
        versionName = "1.0"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
            excludes += "/META-INF/LICENSE*"
        }
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
    buildFeatures {
        buildConfig = true
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }
    testOptions {
        unitTests.isIncludeAndroidResources = true
        unitTests.isReturnDefaultValues = true
        managedDevices {
            localDevices {
                create("pixel7Api34") {
                    device = "Pixel 7"
                    apiLevel = 34
                    systemImageSource = "aosp-atd"
                }
            }
        }
    }
}

dependencies {
    debugImplementation(compose.uiTooling)

    // Firebase Cloud Messaging - use specific version for KMM compatibility
    implementation("com.google.firebase:firebase-messaging-ktx:24.1.0")
    implementation("com.google.firebase:firebase-bom:34.0.0")
}

compose.desktop {
    application {
        mainClass = "com.tt.honeyDue.MainKt"

        nativeDistributions {
            targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
            packageName = "com.tt.honeyDue"
            packageVersion = "1.0.0"
        }
    }
}

// Roborazzi screenshot-regression plugin (parity gallery, P2). Pin the
// golden-image output directory inside the test source set so goldens live
// in git alongside the tests themselves. Anything under build/ is
// gitignored and gets blown away by `gradle clean` — not where committed
// goldens belong.
//
// NOTE on path mismatch: `captureRoboImage(filePath = ...)` in
// ScreenshotTests.kt takes a *relative path* resolved against the Gradle
// test task's working directory (`composeApp/`). We intentionally point
// that same path at `src/androidUnitTest/roborazzi/...` — and configure
// the plugin extension below to match — so record and verify read from
// and write to the exact same committed-golden location. Any other
// arrangement results in the "original file was not found" error because
// the plugin doesn't currently auto-copy between `build/outputs/roborazzi`
// and the extension outputDir for the KMM Android target.
roborazzi {
    outputDir.set(layout.projectDirectory.dir("src/androidUnitTest/roborazzi"))
}
