buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.1.0'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10'
    }
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
    namespace "com.timesafari.dailynotification.plugin"
    compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 35
    
    defaultConfig {
        minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 23
        targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 35

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    
    kotlinOptions {
        jvmTarget = '1.8'
    }
    
    // Enable unit tests with modern AndroidX testing framework
    testOptions {
        unitTests.all {
            enabled = true
        }
        // Enable Android resources for Robolectric (only for test tasks, not all tasks)
        unitTests.includeAndroidResources = true
    }
}

repositories {
    google()
    mavenCentral()
    
    // Try to find Capacitor from node_modules (for standalone builds)
    // In consuming apps, Capacitor will be available as a project dependency
    def capacitorPath = new File(rootProject.projectDir, '../node_modules/@capacitor/android/capacitor')
    if (capacitorPath.exists()) {
        flatDir {
            dirs capacitorPath
        }
    }
}

dependencies {
    // Capacitor dependency - provided by consuming app
    // When included as a project dependency, use project reference
    // NOTE: Capacitor Android is NOT published to Maven - it must be available as a project dependency
    def capacitorProject = project.findProject(':capacitor-android')
    if (capacitorProject != null) {
        implementation capacitorProject
    } else {
        // Capacitor not found - this plugin MUST be built within a Capacitor app context
        // Provide clear error message with instructions
        def errorMsg = """
╔══════════════════════════════════════════════════════════════════╗
║  ERROR: Capacitor Android project not found                      ║
╠══════════════════════════════════════════════════════════════════╣
║                                                                  ║
║  This plugin requires Capacitor Android to build.               ║
║  Capacitor plugins cannot be built standalone.                   ║
║                                                                  ║
║  To build this plugin:                                          ║
║    1. Build from test-apps/android-test-app (recommended)       ║
║       cd test-apps/android-test-app                              ║
║       ./gradlew build                                            ║
║                                                                  ║
║    2. Or include this plugin in a Capacitor app:                 ║
║       - Add to your app's android/settings.gradle:               ║
║         include ':daily-notification-plugin'                    ║
║         project(':daily-notification-plugin').projectDir =       ║
║           new File('../daily-notification-plugin/android')       ║
║                                                                  ║
║  Note: Capacitor Android is only available as a project          ║
║  dependency, not from Maven repositories.                        ║
║                                                                  ║
╚══════════════════════════════════════════════════════════════════╝
"""
        throw new GradleException(errorMsg)
    }
    
    // These dependencies are always available from Maven
    implementation "androidx.appcompat:appcompat:1.7.0"
    implementation "androidx.room:room-runtime:2.6.1"
    implementation "androidx.room:room-ktx:2.6.1"
    implementation "androidx.work:work-runtime-ktx:2.9.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.10"
    implementation "com.google.code.gson:gson:2.10.1"
    implementation "androidx.core:core:1.12.0"
    
    // Room annotation processor - use kapt for Kotlin, annotationProcessor for Java
    kapt "androidx.room:room-compiler:2.6.1"
    annotationProcessor "androidx.room:room-compiler:2.6.1"
    
    // Test dependencies
    testImplementation "junit:junit:4.13.2"
    testImplementation "androidx.test:core:1.5.0"
    testImplementation "androidx.test.ext:junit:1.1.5"
    testImplementation "org.robolectric:robolectric:4.11.1"
    testImplementation "androidx.room:room-testing:2.6.1"
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3"
}

