import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    alias(libs.plugins.androidApplication)
    id("recipe.kotlin.multiplatform")
    alias(libs.plugins.composeMultiplatform)
    alias(libs.plugins.composeCompiler)
    alias(libs.plugins.kotlinSerialization)
    alias(libs.plugins.koin.compiler)
    id("recipe.quality")
}

group = "dev.ulfrx.recipe"
version = "1.0.0"

android {
    namespace = "dev.ulfrx.recipe"
    compileSdk =
        libs.versions.android.compileSdk
            .get()
            .toInt()

    defaultConfig {
        applicationId = "dev.ulfrx.recipe"
        minSdk =
            libs.versions.android.minSdk
                .get()
                .toInt()
        targetSdk =
            libs.versions.android.targetSdk
                .get()
                .toInt()
        versionCode = 1
        versionName = "1.0"

        // Lokksmith's Android redirect activity uses the scheme-only placeholder.
        // Must match `dev.ulfrx.recipe.shared.Constants.OIDC_REDIRECT_URI`.
        manifestPlaceholders["lokksmithRedirectScheme"] = "recipe"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }
}

kotlin {
    androidTarget()
    iosArm64()
    iosSimulatorArm64()

    listOf(targets.getByName("iosArm64"), targets.getByName("iosSimulatorArm64")).forEach { target ->
        (target as KotlinNativeTarget).binaries.framework {
            baseName = "ComposeApp"
            isStatic = true
        }
    }

    sourceSets {
        commonMain.dependencies {
            implementation(projects.shared)

            implementation(project.dependencies.platform(libs.koin.bom))
            implementation(libs.koin.core)
            implementation(libs.koin.compose)
            implementation(libs.koin.composeViewmodel)
            implementation(libs.kermit)
            implementation(libs.compose.runtime)
            implementation(libs.compose.foundation)
            implementation(libs.compose.ui)
            implementation(libs.compose.ui.backhandler)
            implementation(libs.compose.components.resources)
            implementation(libs.compose.uiToolingPreview)
            implementation(libs.androidx.lifecycle.viewmodelCompose)
            implementation(libs.androidx.lifecycle.runtimeCompose)

            implementation(libs.ktor.clientCore)
            implementation(libs.ktor.clientAuth)
            implementation(libs.ktor.clientContentNegotiation)
            implementation(libs.ktor.clientLogging)
            implementation(libs.ktor.serializationKotlinxJsonMpp)
            implementation(libs.kotlinx.serializationJson)
            implementation(libs.kotlinx.datetime)
            implementation(libs.multiplatform.settings)
            implementation(libs.lokksmith.compose)
            implementation(libs.navigation3.ui)
            implementation(libs.androidx.lifecycle.viewmodelNavigation3)
            implementation(libs.compose.unstyled)
            implementation(libs.compose.icons.lucide)
            implementation(libs.liquid)
        }
        androidMain.dependencies {
            implementation(libs.androidx.activity.compose)
            implementation(libs.koin.android)

            implementation(libs.ktor.clientOkhttp)
        }
        iosMain.dependencies {
            // Darwin engine for Ktor. Lokksmith handles the native
            // ASWebAuthenticationSession integration directly from Kotlin.
            implementation(libs.ktor.clientDarwin)
        }
        commonTest.dependencies {
            implementation(libs.kotlin.test)
        }
    }
}

dependencies {
    debugImplementation(libs.compose.uiTooling)
}

compose.resources {
    packageOfResClass = "recipe.composeapp.generated.resources"
}

// The Koin compiler plugin's strict graph check (default `compileSafety = true`) only
// validates types registered via the no-lambda `single<T>()` plugin DSL. Our DI graph
// includes factory-built types (Settings, Lokksmith, HttpClient) that must use the
// traditional `single<T> { ... }` form because they need custom construction. Disable
// the strict check so those lambda-registered types stop tripping false-positive
// "Missing dependency" errors. Runtime resolution is unchanged.
koinCompiler {
    compileSafety = false
}
