use opentelemetry::trace::TracerProvider;
use opentelemetry_sdk::trace::SdkTracerProvider;
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::{EnvFilter, Registry, fmt, layer::SubscriberExt, util::SubscriberInitExt};

/// Initialize tracing with optional OpenTelemetry export.
pub fn init_tracing(service_name: &str, exporter: &str) {
    let env_filter = EnvFilter::try_from_default_env()
        .unwrap_or_else(|_| EnvFilter::new("info"));

    match exporter {
        "stdout" => {
            // OTel traces to stdout + regular fmt layer
            let provider = SdkTracerProvider::builder()
                .with_simple_exporter(opentelemetry_stdout::SpanExporter::default())
                .build();
            let tracer = provider.tracer(service_name.to_string());

            Registry::default()
                .with(env_filter)
                .with(fmt::layer())
                .with(OpenTelemetryLayer::new(tracer))
                .init();

            tracing::info!("tracing initialized: fmt + opentelemetry-stdout");
        }
        _ => {
            // Just fmt layer, no OTel
            Registry::default()
                .with(env_filter)
                .with(fmt::layer())
                .init();

            tracing::info!("tracing initialized: fmt only");
        }
    }
}
