//! Retrieves the `X-Athena-Client` header that selects the Supabase/Postgres client.
use actix_web::HttpRequest;

/// Returns the header value used to decide which downstream client should handle the request.
pub fn x_athena_client(req: &HttpRequest) -> String {
    req.headers()
        .get("X-Athena-Client")
        .and_then(|h| h.to_str().ok())
        .unwrap_or("")
        .to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use actix_web::test::TestRequest;

    #[actix_web::test]
    async fn returns_empty_when_missing() {
        let req: HttpRequest = TestRequest::default().to_http_request();
        assert_eq!(x_athena_client(&req), "");
    }

    #[actix_web::test]
    async fn returns_value_when_present() {
        let req: HttpRequest = TestRequest::default()
            .insert_header(("X-Athena-Client", "supabase"))
            .to_http_request();
        assert_eq!(x_athena_client(&req), "supabase");
    }
}
