fn main() -> Result<(), Box<dyn std::error::Error>> {
    // NOTE: Tonic 0.14+ uses tonic_prost_build instead of tonic_build
    tonic_prost_build::configure()
        .build_server(true)
        .build_client(true) // Enable client for integration tests
        // Suppress warnings in generated code
        .server_mod_attribute(".", "#[allow(unused_qualifications)]")
        .client_mod_attribute(".", "#[allow(unused_qualifications)]")
        .compile_protos(
            &[
                "../../proto/trading.proto",
                "../../proto/risk.proto",
                "../../proto/ml.proto",
                "../../proto/config.proto",
                "../../proto/monitoring.proto",
            ],
            &["../../proto"],
        )?;

    // Compile ml_training.proto (client-only, for retrain_model forwarding)
    tonic_prost_build::configure()
        .build_server(false)
        .build_client(true)
        .client_mod_attribute(".", "#[allow(unused_qualifications)]")
        .compile_protos(
            &["../../proto/ml_training.proto"],
            &["../../proto"],
        )?;

    println!("cargo:rerun-if-changed=../../proto/trading.proto");
    println!("cargo:rerun-if-changed=../../proto/risk.proto");
    println!("cargo:rerun-if-changed=../../proto/ml.proto");
    println!("cargo:rerun-if-changed=../../proto/config.proto");
    println!("cargo:rerun-if-changed=../../proto/monitoring.proto");
    println!("cargo:rerun-if-changed=../../proto/ml_training.proto");

    Ok(())
}
