plugins {
    id 'java-library'
    id 'com.google.protobuf'
    id 'maven-publish'
}

dependencies {
    api "com.google.protobuf:protobuf-java-util:${protobufVersion}"
    api "com.google.protobuf:protobuf-java:${protobufVersion}"
    api "io.grpc:grpc-protobuf:${grpcVersion}"
    api "io.grpc:grpc-stub:${grpcVersion}"

    implementation "com.google.guava:guava:${guavaVersion}"
    implementation "io.grpc:grpc-netty-shaded:${grpcVersion}"

    compileOnly 'javax.annotation:javax.annotation-api:1.3.2'

    testImplementation "com.google.code.gson:gson:${gsonVersion}"
    testImplementation "io.grpc:grpc-inprocess:${grpcVersion}"
    testImplementation "io.grpc:grpc-testing:${grpcVersion}"
}

sourceSets {
    main {
        proto {
            srcDir rootProject.file('../../src/ZB.MOM.WW.MxGateway.Contracts/Protos')
            include 'mxaccess_gateway.proto'
            include 'mxaccess_worker.proto'
            include 'galaxy_repository.proto'
        }
    }
}

java {
    withSourcesJar()
    withJavadocJar()
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:${protobufVersion}"
    }

    plugins {
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
        }
    }

    generatedFilesBaseDir = rootProject.file('src/main/generated').absolutePath

    generateProtoTasks {
        all().configureEach {
            plugins {
                grpc {}
            }
        }
    }
}

// IPC-09 guard: the generated Java tree is tracked (generatedFilesBaseDir above points at the
// committed src/main/generated). `gradle build` regenerates it, so an un-regenerated .proto change,
// or a plugin/protobuf version bump, silently drifts the committed output. checkGeneratedClean
// fails when the regenerated tree differs from what is committed.
//
// The grpc/protobuf toolchain is pinned (build.gradle: grpcVersion / protobufVersion), so a
// regeneration is byte-identical to the committed single-file aggregates modulo real .proto
// changes — no spurious protobuf-runtime-version churn (IPC-24 verified this and deleted the old
// unconditional CI churn-revert step, which masked message-level drift). Regenerate and commit
// after any .proto change. See docs/GatewayTesting.md "Continuous Integration".
tasks.register('checkGeneratedClean') {
    group = 'verification'
    description = 'Fails if the committed generated Java tree differs from a fresh regeneration.'
    dependsOn 'generateProto'
    doLast {
        def generatedDir = 'clients/java/src/main/generated'
        def stdout = new ByteArrayOutputStream()
        def result = exec {
            workingDir = rootProject.projectDir.parentFile.parentFile
            commandLine 'git', 'status', '--porcelain', '--', generatedDir
            standardOutput = stdout
            ignoreExitValue = true
        }
        def dirty = stdout.toString().trim()
        if (!dirty.isEmpty()) {
            throw new GradleException(
                "Generated Java is stale:\n${dirty}\n" +
                "Regenerate and commit the Java client after a .proto change " +
                "(gradle :zb-mom-ww-mxgateway-client:generateProto).")
        }
    }
}
