// Copyright (c) 2026 Gleb Zaharov. License: GPLv3 (see LICENSE).

import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.Properties

plugins {
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.jetbrainsKotlinAndroid)
    alias(libs.plugins.kotlin.compose)
    id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    id("com.google.devtools.ksp")
    id("io.sentry.android.gradle") version "5.5.0"
}

secrets {
    propertiesFileName = "secrets.properties"
}

android {
    namespace = "ru.sweetbread.unn"
    compileSdk = 36

    defaultConfig {
        applicationId = "ru.sweetbread.unn"
        minSdk = 26
        targetSdk = 36
        versionCode = 1
        versionName = "1.0"

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

        val secretProperties = Properties().apply {
            val secretFile = rootProject.file("secrets.properties")
            if (secretFile.exists())
                secretFile.inputStream().use { load(it) }
            else
                println("Warning: secrets.properties not found!")
        }
        manifestPlaceholders["sentry_url"] = secretProperties.getProperty("SENTRY_URL")!!

//        javaCompileOptions {
//            annotationProcessorOptions {
//                arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
//            }
//        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
            resValue("string", "app_name", "@string/app_name_reg")
            manifestPlaceholders["sentry_env"] = "production"
        }
        debug {
            applicationIdSuffix = ".debug"
            versionNameSuffix =
                "-debug+${LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE)}"
            resValue("string", "app_name", "@string/app_name_dev")
            manifestPlaceholders["sentry_env"] = "debug"
        }
        create("beta") {
            versionNameSuffix =
                "-beta+${LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE)}"
            resValue("string", "app_name", "@string/app_name_beta")
            signingConfig = signingConfigs.getByName("debug")
            manifestPlaceholders["sentry_env"] = "beta"
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    buildFeatures {
        compose = true
        viewBinding = true
        buildConfig = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

kotlin {
    compilerOptions {
        jvmTarget = JvmTarget.JVM_1_8
    }
}

base {
    archivesName = "ru.sweetbread.unn-v${android.defaultConfig.versionCode}(${android.defaultConfig.versionName})"
}

dependencies {
    implementation(libs.androidx.material.icons.core.android)
    coreLibraryDesugaring(libs.desugar.jdk.libs)

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.core.splashscreen)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    implementation(libs.androidx.appcompat)
    implementation(libs.material)
    implementation(libs.androidx.annotation)
    implementation(libs.androidx.constraintlayout)
    implementation(libs.androidx.lifecycle.livedata.ktx)
    implementation(libs.androidx.lifecycle.viewmodel.ktx)
    implementation(libs.androidx.activity)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
    implementation(libs.androidx.navigation.compose)

    implementation(libs.ktor.client.core)
    implementation(libs.ktor.client.cio)
    implementation(libs.ktor.client.logging)
    implementation(libs.ktor.client.android)
    implementation(libs.coil.compose)

    implementation(libs.androidx.datastore.preferences)
    implementation(libs.splitties.base)
    implementation(libs.splitties.room)

    implementation(libs.sentry)

    implementation(libs.androidx.room.runtime)
    implementation(libs.androidx.room.ktx)
    ksp(libs.androidx.room.compiler)
}
