//! Athena-native backend identity helpers for metrics and traces.

/// Normalizes adapter, provider, and legacy labels into Athena engine labels.
pub fn normalize_athena_backend_engine_label(label: &str) -> Option<&'static str> {
    let normalized = label.trim().to_ascii_lowercase();
    match normalized.as_str() {
        "" => None,
        "postgres" | "postgresql" | "pg" | "sqlx" | "deadpool" | "athena" | "supabase"
        | "custom_supabase" => Some("postgres"),
        "d1" | "cloudflare-d1" | "cloudflare_d1" => Some("d1"),
        "scylla" | "scylladb" | "cql" => Some("scylla"),
        "s3" | "minio" | "digitalocean" | "digitalocean-spaces" | "hetzner" => Some("s3"),
        _ => None,
    }
}

/// Returns the default Athena engine for gateway routes that do not attach an explicit backend.
pub fn default_gateway_backend_engine_label(route_or_operation: &str) -> Option<&'static str> {
    let normalized = route_or_operation.trim().to_ascii_lowercase();
    if normalized.is_empty() {
        return None;
    }

    if normalized == "query"
        || normalized == "fetch"
        || normalized == "fetch_get"
        || normalized == "insert"
        || normalized == "update"
        || normalized == "delete"
        || normalized == "rpc"
        || normalized == "/data"
        || normalized.starts_with("/gateway/")
        || normalized.starts_with("/rest/")
        || normalized.starts_with("/rpc/")
    {
        return Some("postgres");
    }

    None
}

/// Resolves the Athena engine label for one route/operation plus an optional raw backend label.
pub fn gateway_backend_engine_label(
    route_or_operation: &str,
    raw_backend_label: Option<&str>,
) -> &'static str {
    raw_backend_label
        .and_then(normalize_athena_backend_engine_label)
        .or_else(|| default_gateway_backend_engine_label(route_or_operation))
        .unwrap_or("unknown")
}

#[cfg(test)]
mod tests {
    use super::{
        default_gateway_backend_engine_label, gateway_backend_engine_label,
        normalize_athena_backend_engine_label,
    };

    #[test]
    fn normalizes_adapter_and_provider_labels_to_athena_engines() {
        assert_eq!(
            normalize_athena_backend_engine_label("sqlx"),
            Some("postgres")
        );
        assert_eq!(
            normalize_athena_backend_engine_label("cloudflare-d1"),
            Some("d1")
        );
        assert_eq!(
            normalize_athena_backend_engine_label("scylladb"),
            Some("scylla")
        );
        assert_eq!(normalize_athena_backend_engine_label(""), None);
    }

    #[test]
    fn defaults_gateway_routes_to_postgres_when_backend_is_missing() {
        assert_eq!(
            default_gateway_backend_engine_label("/gateway/fetch"),
            Some("postgres")
        );
        assert_eq!(
            default_gateway_backend_engine_label("delete"),
            Some("postgres")
        );
        assert_eq!(default_gateway_backend_engine_label("/metrics"), None);
    }

    #[test]
    fn route_fallback_covers_bad_backend_labels() {
        assert_eq!(
            gateway_backend_engine_label("fetch", Some("tenant_alpha")),
            "postgres"
        );
        assert_eq!(
            gateway_backend_engine_label("/gateway/query", Some("sqlx")),
            "postgres"
        );
        assert_eq!(gateway_backend_engine_label("custom", None), "unknown");
    }
}
