fn main() -> Result<(), Box<dyn std::error::Error>> {
    // proto/ at workspace root — single source of truth for all services.
    // Note: fxt_trading.proto (package foxhunt.tli) is excluded — it is the
    // legacy fat-client proto kept for wire compatibility (package name retained).
    let proto_root = "../../proto";
    let protos: Vec<String> = [
        "auth",
        "trading",
        "health",
        "ml",
        "config",
        "ml_training",
        "trading_agent",
        "broker_gateway",
        "monitoring",
        "risk",
        "data_acquisition",
    ]
    .iter()
    .map(|p| format!("{proto_root}/{p}.proto"))
    .collect();

    tonic_prost_build::configure()
        .build_server(true) // Required for E2E test mock servers
        .build_client(true)
        .type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]")
        .server_mod_attribute(".", "#[allow(unused_qualifications)]")
        .client_mod_attribute(".", "#[allow(unused_qualifications)]")
        .protoc_arg("--experimental_allow_proto3_optional")
        .compile_protos(&protos, &[proto_root.to_owned()])?;

    for proto in &[
        "auth",
        "trading",
        "health",
        "ml",
        "config",
        "ml_training",
        "trading_agent",
        "broker_gateway",
        "monitoring",
        "risk",
        "data_acquisition",
    ] {
        println!("cargo:rerun-if-changed={proto_root}/{proto}.proto");
    }

    Ok(())
}
