//! Marketplace listing cache.
//!
//! Key: `jobs:marketplace:{profession}:{page}:{limit}` → JSON, TTL 5 min

use redis::AsyncCommands;
use crate::RedisPool;

const CACHE_TTL: u64 = 300; // 5 minutes

pub async fn get_marketplace(
    redis: &mut RedisPool,
    profession: &str,
    page: i64,
    limit: i64,
) -> Result<Option<String>, redis::RedisError> {
    let key = format!("jobs:marketplace:{profession}:{page}:{limit}");
    redis.get(key).await
}

pub async fn set_marketplace(
    redis: &mut RedisPool,
    profession: &str,
    page: i64,
    limit: i64,
    data: &str,
) -> Result<(), redis::RedisError> {
    let key = format!("jobs:marketplace:{profession}:{page}:{limit}");
    redis.set_ex(key, data, CACHE_TTL).await
}

/// Invalidate all marketplace caches for a profession (call when a profile is updated).
pub async fn invalidate_marketplace(
    redis: &mut RedisPool,
    profession: &str,
) -> Result<(), redis::RedisError> {
    let pattern = format!("jobs:marketplace:{profession}:*");
    let keys: Vec<String> = redis.keys(pattern).await?;
    if !keys.is_empty() {
        redis.del::<_, ()>(keys).await?;
    }
    Ok(())
}
