ext {
    junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
    androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
    androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.3.0'
    androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.0'

    // Read provider configuration from gradle.properties (set by hook script)
    // These need to be at ext level so they can be used in sourceSets
    includeFacebook = project.findProperty('socialLogin.facebook.include') ?: 'true'
}

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.13.0'
    }
}

apply plugin: 'com.android.library'

android {
    namespace = "ee.forgr.capacitor.social.login"
    compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
    defaultConfig {
        minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
        targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'consumer-proguard-rules.pro'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError = false
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_21
        targetCompatibility JavaVersion.VERSION_21
    }

    // Compile either the real Facebook SDK provider or a plugin-namespace stub.
    // Disabled builds must not ship com.facebook.* classes (privacy scanners).
    sourceSets {
        main {
            if (includeFacebook == 'true') {
                java.srcDirs += 'src/facebookEnabled/java'
            } else {
                java.srcDirs += 'src/facebookDisabled/java'
            }
        }
    }
}

repositories {
    google()
    mavenCentral()
}


dependencies {
    // Common dependencies (always included)
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':capacitor-android')
    implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
    implementation 'com.squareup.okhttp3:okhttp:4.12.0'
    implementation 'com.auth0.android:jwtdecode:2.0.2'
    implementation 'androidx.credentials:credentials:1.5.0'
    implementation 'androidx.concurrent:concurrent-futures:1.3.0'
    implementation 'androidx.browser:browser:1.9.0'
    
    // Read provider configuration from gradle.properties (set by hook script)
    def googleDependencyType = project.findProperty('socialLogin.google.dependencyType') ?: 'implementation'
    def includeGoogle = project.findProperty('socialLogin.google.include') ?: 'true'
    
    // Note: includeFacebook is defined in ext block at the top
    
    def appleDependencyType = project.findProperty('socialLogin.apple.dependencyType') ?: 'implementation'
    def includeApple = project.findProperty('socialLogin.apple.include') ?: 'true'
    
    def twitterDependencyType = project.findProperty('socialLogin.twitter.dependencyType') ?: 'implementation'
    def includeTwitter = project.findProperty('socialLogin.twitter.include') ?: 'true'
    
    // Google dependencies
    if (googleDependencyType == 'compileOnly') {
        compileOnly 'com.google.android.gms:play-services-auth:21.4.0'
        compileOnly "androidx.credentials:credentials-play-services-auth:1.5.0"
        compileOnly "com.google.android.libraries.identity.googleid:googleid:1.1.1"
        compileOnly('com.google.androidbrowserhelper:androidbrowserhelper:2.5.0') {
            exclude group: 'androidx.browser', module: 'browser'
        }
    } else if (includeGoogle == 'true') {
        implementation 'com.google.android.gms:play-services-auth:21.4.0'
        implementation "androidx.credentials:credentials-play-services-auth:1.5.0"
        implementation "com.google.android.libraries.identity.googleid:googleid:1.1.1"
        implementation('com.google.androidbrowserhelper:androidbrowserhelper:2.5.0') {
            exclude group: 'androidx.browser', module: 'browser'
        }
    }

    // Facebook dependencies
    // When Facebook is disabled, FacebookProvider from src/facebookDisabled is used instead
    if (includeFacebook == 'true') {
        implementation 'com.facebook.android:facebook-login:18.1.3'
    }
    // When includeFacebook != 'true', no Facebook SDK and no com.facebook.* stubs
    
    // Apple dependencies
    if (appleDependencyType == 'compileOnly') {
        compileOnly "androidx.browser:browser:1.9.0"
    } else if (includeApple == 'true') {
        implementation "androidx.browser:browser:1.9.0"
    }
    
    // Twitter dependencies (uses OAuth flow, no external SDK)
    // Twitter uses standard OAuth 2.0 flow without external dependencies
    
    // Test dependencies (always included)
    testImplementation "junit:junit:$junitVersion"
    testImplementation "org.json:json:20240303"
    androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
}
