import java.util.Properties
import java.util.Date

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.compose)
    alias(libs.plugins.ksp)
    alias(libs.plugins.secrets)
    alias(libs.plugins.google.services)
    alias(libs.plugins.firebase.appdistribution)
}

android {
    namespace = "com.example.scanwich"
    compileSdk = 36

    defaultConfig {
        applicationId = "com.example.scanwich"
        minSdk = 24
        targetSdk = 35
        
        // Incrémentation automatique du versionCode basé sur le temps
        versionCode = (System.currentTimeMillis() / 60000).toInt() 
        versionName = "1.0." + (System.currentTimeMillis() / 3600000).toString().takeLast(3)

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

    signingConfigs {
        // Chargement des propriétés depuis local.properties
        val keystoreProperties = Properties()
        val keystorePropertiesFile = rootProject.file("local.properties")
        if (keystorePropertiesFile.exists()) {
            keystoreProperties.load(keystorePropertiesFile.inputStream())
        }

        getByName("debug") {
            storeFile = file(System.getProperty("user.home") + "/.android/debug.keystore")
            getByName("debug").storePassword = "android"
            getByName("debug").keyAlias = "androiddebugkey"
            getByName("debug").keyPassword = "android"
        }

        create("release") {
            storeFile = file("C:\\Users\\mac\\keys\\keys")
            storePassword = keystoreProperties.getProperty("RELEASE_STORE_PASSWORD")
            keyAlias = keystoreProperties.getProperty("RELEASE_KEY_ALIAS") ?: "key0"
            keyPassword = keystoreProperties.getProperty("RELEASE_KEY_PASSWORD")
        }
    }

    val keyFile = project.file("firebase-key.json")
    val releaseNotesFile = rootProject.file("release-notes.txt")

    buildTypes {
        release {
            isMinifyEnabled = true // Activer l'offuscation
            signingConfig = signingConfigs.getByName("release")
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
            
            configure<com.google.firebase.appdistribution.gradle.AppDistributionExtension> {
                artifactType = "APK"
                if (keyFile.exists()) {
                    serviceCredentialsFile = keyFile.absolutePath
                }
                if (releaseNotesFile.exists()) {
                    releaseNotes = releaseNotesFile.readText()
                }
                groups = "internal-user"
            }
        }
        
        debug {
            isMinifyEnabled = false
            configure<com.google.firebase.appdistribution.gradle.AppDistributionExtension> {
                artifactType = "APK"
                if (keyFile.exists()) {
                    serviceCredentialsFile = keyFile.absolutePath
                }
                if (releaseNotesFile.exists()) {
                    releaseNotes = releaseNotesFile.readText()
                }
                groups = "internal-user"
            }
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }
    buildFeatures {
        compose = true
        buildConfig = true
    }
}

secrets {
    ignoreList.add("properties")
}

dependencies {
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    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)
    
    // SDK Firebase App Distribution COMPLET (API + Implémentation)
    implementation(libs.firebase.appdistribution)

    implementation(libs.google.generativeai)
    implementation(libs.coil.compose)
    implementation(libs.androidx.exifinterface)
    implementation(libs.androidx.navigation.compose)
    implementation(libs.androidx.room.runtime)
    implementation(libs.androidx.room.ktx)
    ksp(libs.androidx.room.compiler)
    implementation(libs.retrofit.core)
    implementation(libs.retrofit.gson)
    implementation(libs.okhttp.logging)
    implementation(libs.androidx.browser)
    implementation(libs.play.services.auth)
    implementation(platform(libs.firebase.bom))
    implementation(libs.firebase.analytics)
    implementation(libs.firebase.functions)
    implementation(libs.firebase.auth)
    implementation(libs.firebase.appcheck.playintegrity)

    // PDF generation
    implementation(libs.itext7.core)
    
    // Security
    implementation(libs.androidx.security.crypto)

    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)
    debugImplementation(libs.firebase.appcheck.debug)
}
