buildscript {
    ext.kotlin_version = '1.8.22'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"

    // Ajuste de dependencias
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support' && !details.requested.name.contains('multidex')) {
                details.useVersion "27.1.1"
            }
            if (details.requested.group == "org.jetbrains.kotlin") {
                details.useVersion "1.8.22"
            }
        }
    }

    // Ajuste del namespace y eliminación del atributo 'package' en manifests
    project.plugins.withId("com.android.library") {
        project.android {
            if (namespace == null || namespace.isEmpty()) {
                namespace = "${project.group}.${project.name}".replace('-', '_')
            }

            project.tasks.configureEach { task ->
                if (task.name.contains("processDebugManifest") || task.name.contains("processReleaseManifest")) {
                    task.doFirst {
                        File manifestFile = file("${projectDir}/src/main/AndroidManifest.xml")
                        if (manifestFile.exists()) {
                            String manifestContent = manifestFile.text
                            if (manifestContent.contains('package=')) {
                                manifestContent = manifestContent.replaceAll(/package="[^"]*"/, "")
                                manifestFile.write(manifestContent)
                                println "Removed 'package' attribute from ${manifestFile}"
                            }
                        }
                    }
                }
            }
        }
    }

    // Fuerza Java 17 en proyectos Android
    project.plugins.withId("com.android.application") {
        project.android {
            compileOptions {
                sourceCompatibility JavaVersion.VERSION_17
                targetCompatibility JavaVersion.VERSION_17
            }
        }
    }

    project.plugins.withId("com.android.library") {
        project.android {
            compileOptions {
                sourceCompatibility JavaVersion.VERSION_17
                targetCompatibility JavaVersion.VERSION_17
            }
        }
    }

    // Fuerza Kotlin JVM target a 17
    project.plugins.withId("org.jetbrains.kotlin.jvm") {
        project.tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
            kotlinOptions {
                jvmTarget = "17"
            }
        }
    }

    project.plugins.withId("org.jetbrains.kotlin.android") {
        project.tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
            kotlinOptions {
                jvmTarget = "17"
            }
        }
    }

    // Fuerza Java para proyectos con plugin 'java'
    project.plugins.withId("java") {
        java {
            toolchain {
                languageVersion = JavaLanguageVersion.of(17)
            }
        }
    }

    // Fuerza nivel de compatibilidad en tareas Java
    project.tasks.withType(JavaCompile).configureEach {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
}

subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}
