plugins {
    id 'java'
    id 'org.springframework.boot' version '3.3.5' apply false
    id 'io.spring.dependency-management' version '1.1.6' apply false
    id 'io.freefair.lombok' version '8.10' apply false
}

group = 'com.unicorn.hgzero'
version = '1.0.0'

allprojects {
    repositories {
        mavenCentral()
        gradlePluginPortal()
    }
}

subprojects {
    apply plugin: 'java'
    apply plugin: 'io.freefair.lombok'

    java {
        sourceCompatibility = JavaVersion.VERSION_21
        targetCompatibility = JavaVersion.VERSION_21
    }

    configurations {
        compileOnly {
            extendsFrom annotationProcessor
        }
    }

    tasks.named('test') {
        useJUnitPlatform()
    }

    // Common versions for all subprojects
    ext {
        jjwtVersion = '0.12.5'
        springdocVersion = '2.5.0'
        mapstructVersion = '1.5.5.Final'
        commonsLang3Version = '3.14.0'
        commonsIoVersion = '2.16.1'
        hypersistenceVersion = '3.7.3'
        openaiVersion = '0.18.2'
        feignJacksonVersion = '13.1'
        azureSpeechVersion = '1.37.0'
        azureBlobVersion = '12.25.3'
        azureEventHubsVersion = '5.18.2'
        azureEventHubsCheckpointVersion = '1.19.2'
        azureAiSearchVersion = '11.6.2'
        springAiVersion = '1.0.0-M1'
    }
}

// Configure all subprojects with Spring dependency management
subprojects {
    apply plugin: 'io.spring.dependency-management'

    dependencyManagement {
        imports {
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:2023.0.2"
        }
    }
}

// Configure only service modules (exclude common)
configure(subprojects.findAll { it.name != 'common' }) {
    apply plugin: 'org.springframework.boot'

    dependencies {
        // Common module dependency
        implementation project(':common')

        // Spring Boot Starters
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        implementation 'org.springframework.boot:spring-boot-starter-security'
        implementation 'org.springframework.boot:spring-boot-starter-validation'

        // Actuator for health checks and monitoring
        implementation 'org.springframework.boot:spring-boot-starter-actuator'

        // API Documentation (common across all services)
        implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:${springdocVersion}"

        // JWT
        implementation "io.jsonwebtoken:jjwt-api:${jjwtVersion}"
        runtimeOnly "io.jsonwebtoken:jjwt-impl:${jjwtVersion}"
        runtimeOnly "io.jsonwebtoken:jjwt-jackson:${jjwtVersion}"

        // Database
        runtimeOnly 'org.postgresql:postgresql'
        implementation 'org.flywaydb:flyway-core'
        runtimeOnly 'org.flywaydb:flyway-database-postgresql'

        // Redis
        implementation 'org.springframework.boot:spring-boot-starter-data-redis'

        // Utilities
        implementation "org.apache.commons:commons-lang3:${commonsLang3Version}"
        implementation "commons-io:commons-io:${commonsIoVersion}"
        implementation "org.mapstruct:mapstruct:${mapstructVersion}"
        annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"

        // Testing
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
        testImplementation 'org.springframework.security:spring-security-test'
        testImplementation 'org.testcontainers:junit-jupiter'
        testImplementation 'org.mockito:mockito-junit-jupiter'

        // Configuration Processor
        annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    }
}

// Java version consistency check for all modules
tasks.register('checkJavaVersion') {
    doLast {
        println "Java Version: ${System.getProperty('java.version')}"
        println "Java Home: ${System.getProperty('java.home')}"
    }
}

// Clean task for all subprojects
tasks.register('cleanAll') {
    dependsOn subprojects.collect { it.tasks.named('clean') }
    description = 'Clean all subprojects'
}

// Build task for all subprojects
tasks.register('buildAll') {
    dependsOn subprojects.collect { it.tasks.named('build') }
    description = 'Build all subprojects'
}
