//! ### Cache-Control
//!
//! This module provides functionality to check the "Cache-Control" header in HTTP requests.

use actix_web::HttpRequest;

/// ## Check Cache-Control Header
///
/// This function checks if the "Cache-Control" header in the request is set to "no-cache".
///
/// ### Parameters
///
/// - `req`: A reference to the `HttpRequest` object.
///
/// ### Returns
///
/// - `bool`: Returns `true` if the "Cache-Control" header is set to "no-cache", otherwise returns `false`.
///
/// ### Example
///
/// ```rust,no_run
/// # use actix_web::test::TestRequest;
/// # use athena_rs::api::cache::cache_control::is_cache_control_no_cache;
/// let req = TestRequest::default()
///     .insert_header(("Cache-Control", "no-cache"))
///     .to_http_request();
/// let no_cache = is_cache_control_no_cache(&req);
/// assert!(no_cache);
/// ```
pub fn is_cache_control_no_cache(req: &HttpRequest) -> bool {
    req.headers()
        .get("Cache-Control")
        .map_or(false, |value| value == "no-cache")
}

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

    #[actix_web::test]
    async fn test_no_cache_true_when_header_present() {
        let req = TestRequest::default()
            .insert_header(("Cache-Control", "no-cache"))
            .to_http_request();
        assert!(is_cache_control_no_cache(&req));
    }

    #[actix_web::test]
    async fn test_no_cache_false_when_header_missing() {
        let req = TestRequest::default().to_http_request();
        assert!(!is_cache_control_no_cache(&req));
    }

    #[actix_web::test]
    async fn test_no_cache_false_other_directives() {
        let req = TestRequest::default()
            .insert_header(("Cache-Control", "max-age=60"))
            .to_http_request();
        assert!(!is_cache_control_no_cache(&req));
    }
}
