use std::path::PathBuf;
use crate::{CacheManager, HttpResponse, Result};
use http_cache_semantics::CachePolicy;
use serde::{Deserialize, Serialize};
#[cfg_attr(docsrs, doc(cfg(feature = "manager-cacache")))]
#[derive(Debug, Clone)]
pub struct CACacheManager {
    pub path: PathBuf,
}
impl Default for CACacheManager {
    fn default() -> Self {
        Self { path: "./http-cacache".into() }
    }
}
#[derive(Debug, Deserialize, Serialize)]
struct Store {
    response: HttpResponse,
    policy: CachePolicy,
}
#[allow(dead_code)]
impl CACacheManager {
    pub async fn clear(&self) -> Result<()> {
        cacache::clear(&self.path).await?;
        Ok(())
    }
}
#[async_trait::async_trait]
impl CacheManager for CACacheManager {
    async fn get(
        &self,
        cache_key: &str,
    ) -> Result<Option<(HttpResponse, CachePolicy)>> {
        let store: Store = match cacache::read(&self.path, cache_key).await {
            Ok(d) => bincode::deserialize(&d)?,
            Err(_e) => {
                return Ok(None);
            }
        };
        Ok(Some((store.response, store.policy)))
    }
    async fn put(
        &self,
        cache_key: String,
        response: HttpResponse,
        policy: CachePolicy,
    ) -> Result<HttpResponse> {
        let data = Store { response: response.clone(), policy };
        let bytes = bincode::serialize(&data)?;
        cacache::write(&self.path, cache_key, bytes).await?;
        Ok(response)
    }
    async fn delete(&self, cache_key: &str) -> Result<()> {
        Ok(cacache::remove(&self.path, cache_key).await?)
    }
}