//! Lead request deduplication.
//!
//! Prevents a professional from sending more than one lead request
//! to the same requirement within 24 hours.
//!
//! Key: `lead_dedup:{professional_id}:{requirement_id}`  → "1", TTL 24 h

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

const DEDUP_TTL: u64 = 24 * 3_600; // 24 hours

/// Returns `true` if the professional has already sent a lead request
/// for this requirement in the last 24 hours.
pub async fn is_duplicate(
    redis: &mut RedisPool,
    professional_id: &str,
    requirement_id: &str,
) -> Result<bool, redis::RedisError> {
    let key = format!("lead_dedup:{professional_id}:{requirement_id}");
    let exists: bool = redis.exists(key).await?;
    Ok(exists)
}

/// Mark a lead request as sent. Call after successfully creating it in the DB.
pub async fn mark_sent(
    redis: &mut RedisPool,
    professional_id: &str,
    requirement_id: &str,
) -> Result<(), redis::RedisError> {
    let key = format!("lead_dedup:{professional_id}:{requirement_id}");
    redis.set_ex(key, "1", DEDUP_TTL).await
}
