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

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

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