use crate::cache::{get_hash, get_str, BB8Pool, CacheError};
use cal_core::RegionLite;

pub async fn get_region_by_ident_lite(
    pool: &BB8Pool,
    key: &str,
) -> Result<Option<RegionLite>, CacheError> {
    let result = get_hash(pool, "cb:regions", key).await;
    match result {
        Ok(r) => match r {
            None => {
                println!("get_region_by_ident_lite({}) Redis Result None", key);
                Ok(None)
            }
            Some(value) => {
                println!("get_region_by_ident_lite({}) Redis Result {}", key, value);
                get_region_by_id_lite(pool, &value).await
            }
        },
        Err(e) => Err(e),
    }
}

pub async fn get_region_by_id_lite(
    pool: &BB8Pool,
    key: &str,
) -> Result<Option<RegionLite>, CacheError> {
    let _key = format!("cb:region:{}", key);
    let result = get_str(pool, &_key).await;
    match result {
        Ok(r) => match r {
            None => {
                println!("get_region_by_id_lite({}) Redis Result None", &_key);
                Ok(None)
            }
            Some(value) => {
                println!("get_region_by_id_lite({}) Redis Result {}", value, &_key);
                let region: RegionLite = serde_json::from_str(&value).unwrap();
                Ok(Some(region))
            }
        },
        Err(e) => Err(e),
    }
}
