import org.jetbrains.kotlin.gradle.dsl.KotlinVersion

/*
 * MIT License
 *
 * Copyright (c) 2022 Ahmed Tikiwa
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.kotlin.compose.compiler)
    alias(libs.plugins.ksp)
    alias(libs.plugins.ktlint)
    alias(libs.plugins.gms)
    alias(libs.plugins.firebase.crashlytics.gradle)
    alias(libs.plugins.firebase.perf.gradle)
    alias(libs.plugins.hilt.gradle)
    alias(libs.plugins.baseline.profiles)

    id("kotlin-kapt")
    id("kotlin-parcelize")
}

def localProperties = new Properties()
localProperties.load(new FileInputStream(rootProject.file("local.properties")))

android {
    namespace = "com.theupnextapp"

    compileSdk 36

    defaultConfig {
        applicationId "com.theupnextapp"
        minSdkVersion 24
        targetSdkVersion 36
        versionCode 209
        versionName "3.10.2"
        testInstrumentationRunner "com.theupnextapp.CustomTestRunner"

        ksp {
            arg("room.schemaLocation", "$projectDir/schemas".toString())
        }
    }

    sourceSets {
        androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
    }

    applicationVariants.configureEach { variant ->
        kotlin.sourceSets {
            getByName(variant.name) {
                kotlin.srcDir("build/generated/ksp/${variant.name}/kotlin")
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    buildTypes.each {
        it.buildConfigField('String', 'TRAKT_CLIENT_ID', localProperties.getProperty("TraktClientID"))
        it.buildConfigField('String', 'TRAKT_CLIENT_SECRET', localProperties.get("TraktClientSecret"))
        it.buildConfigField('String', 'TRAKT_REDIRECT_URI', localProperties.getProperty("TraktRedirectURI"))
    }

    buildFeatures {
        dataBinding true
        buildConfig true
        compose true
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_17
    }

    kotlin {
        jvmToolchain(17)

        compilerOptions {
            languageVersion.set(KotlinVersion.KOTLIN_1_9)
            apiVersion.set(KotlinVersion.KOTLIN_1_9)
        }
    }

    ksp {
        arg("compose-destinations.generateNavGraphs", "true")
    }

    hilt {
        enableAggregatingTask = true
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }
}

dependencies {
    implementation libs.appcompat
    implementation libs.android.ktx
    implementation libs.constraintlayout
    implementation libs.android.material
    implementation libs.android.material3.adaptive
    implementation libs.android.material3.adaptive.layout
    implementation libs.android.material3.adaptive.navigation
    implementation libs.android.material3.adaptive.navigation.suite
    implementation libs.androidx.adaptive.navigation.android

    // Compose
    // Integration with activities
    implementation libs.activity.compose
    // Compose Material Design
    implementation libs.android.material3
    implementation libs.material3.window.size.classes
    // Animations
    implementation libs.animation
    // Tooling support (Previews, etc.)
    implementation libs.androidx.ui.tooling
    // Integration with ViewModels
    implementation libs.lifecycle.viewmodel.compose
    // Compose Coil
    implementation libs.coil.compose
    // Hilt Navigation
    implementation libs.android.hilt.navigation.compose
    // Compose Livedata runtime
    implementation libs.runtime.livedata
    // Compose Icons Extended
    implementation libs.material.icons.extended

    // Coroutines for getting off the UI thread
    implementation libs.kotlinx.coroutines.core
    implementation libs.kotlinx.coroutines.android

    // Retrofit for networking
    implementation libs.retrofit
    implementation libs.converter.moshi
    implementation libs.retrofit2.kotlin.coroutines.adapter

    // Moshi for parsing the JSON format
    implementation libs.moshi
    implementation libs.moshi.kotlin

    // OkHTTP for Retrofit
    implementation libs.okhttp
    implementation libs.logging.interceptor

    // Glide for images
    implementation libs.glide
    implementation libs.legacy.support.v4
    ksp libs.glide.compiler

    // ViewModel and LiveData (arch components)
    implementation libs.lifecycle.livedata.ktx
    implementation libs.lifecycle.common.java8
    implementation libs.lifecycle.viewmodel.ktx

    // Data store
    implementation libs.datastore.preferences

    // Room database
    implementation libs.room.runtime
    ksp libs.room.compiler
    implementation libs.room.ktx

    // Hilt Work
    implementation libs.android.hilt.work
    ksp libs.android.hilt.work.compiler
    implementation libs.work.runtime.ktx

    // Hilt
    implementation libs.dagger.hilt.android
    ksp libs.hilt.android.compiler

    // Timber
    // Logging
    implementation libs.timber

    // Gson
    implementation libs.converter.gson

    // Jsoup
    implementation libs.jsoup

    implementation libs.firebase.core

    // Firebase Analytics (Kotlin)
    implementation libs.firebase.analytics.ktx

    // Crashlytics
    implementation libs.firebase.crashlytics

    // Recyclerview
    implementation libs.android.recyclerview
    // For control over item selection of both touch and mouse driven selection
    implementation libs.recyclerview.selection

    // Performance Monitoring
    implementation libs.firebase.perf

    // PreferenceManager
    implementation libs.preference.ktx

    // Leak canary
    debugImplementation libs.leakcanary.android

    // Paging
    implementation libs.paging.runtime.ktx

    // Chrome Custom Tabs
    implementation libs.browser

    // Flexbox
    implementation libs.flexbox

    // Jetpack Compose Destinations
    implementation libs.compose.destinations.core
    ksp libs.compose.destinations.compiler

    // Accompanist Navigation Animation
    implementation libs.accompanist.navigation.animation

    // Shimmer
    implementation libs.compose.shimmer

    // SwipeRefresh
    implementation libs.accompanist.swiperefresh

    // Baseline Profiles
    implementation libs.profileinstaller
    baselineProfile project(path: ':baselineprofile')

    debugImplementation libs.chucker.debug
    releaseImplementation libs.chucker.release

    // Test harness
    implementation libs.test.harness
    debugImplementation(libs.androidx.ui.test.manifest)

    testImplementation libs.junit
    androidTestImplementation libs.androidx.test.ext.junit
    androidTestImplementation libs.androidx.ui.test.junit4
    androidTestImplementation libs.androidx.test.runner
    androidTestImplementation libs.room.testing

    // Mockito for mocking dependencies (highly recommended)
    testImplementation libs.mockito.core
    testImplementation libs.mockito.kotlin

    testImplementation libs.kotlinx.coroutines.test
    testImplementation libs.androidx.arch.core.testing

    // Hilt testing
    androidTestImplementation libs.hilt.android.testing
    kspAndroidTest libs.hilt.android.compiler
    androidTestAnnotationProcessor libs.hilt.android.compiler

    // WorkManager testing
    androidTestImplementation libs.androidx.work.testing
}