//! Content Defense Keys (Phase 7C) — near-duplicate detection and quarantine.

use super::builders::global_key;

/// MinHash signature key: `\x00MH:{content_hash_hex}`
///
/// Stores the MinHash signature for an assertion's subject+predicate content.
/// Used for near-duplicate detection via LSH.
pub fn minhash_key(content_hash_hex: &str) -> Vec<u8> {
    global_key(b"MH:", content_hash_hex.as_bytes())
}

/// MinHash scan prefix: `\x00MH:`
///
/// Scan all MinHash signatures (used for Bloom filter rebuild on startup).
pub fn minhash_scan_prefix() -> Vec<u8> {
    global_key(b"MH:", b"")
}

/// LSH bucket key: `\x00LSH:{band:02}:{bucket_hash_hex}`
///
/// Stores the list of assertion hashes in an LSH bucket for a given band.
/// Band number is zero-padded to 2 digits (00-15) for consistent ordering.
pub fn lsh_bucket_key(band: u8, bucket_hash_hex: &str) -> Vec<u8> {
    let suffix = format!("{:02}:{}", band, bucket_hash_hex);
    global_key(b"LSH:", suffix.as_bytes())
}

/// LSH band scan prefix: `\x00LSH:{band:02}:`
///
/// Scan all buckets for a specific band.
pub fn lsh_band_scan_prefix(band: u8) -> Vec<u8> {
    let suffix = format!("{:02}:", band);
    global_key(b"LSH:", suffix.as_bytes())
}

/// LSH scan prefix: `\x00LSH:`
///
/// Scan all LSH bucket entries.
pub fn lsh_scan_prefix() -> Vec<u8> {
    global_key(b"LSH:", b"")
}

/// Quarantine key: `\x00QUAR:{timestamp}:{hash_hex}`
///
/// Stores a quarantined assertion awaiting admin review.
/// Timestamp prefix enables time-ordered scanning (oldest first).
pub fn quarantine_key(timestamp: u64, hash_hex: &str) -> Vec<u8> {
    let suffix = format!("{}:{}", timestamp, hash_hex);
    global_key(b"QUAR:", suffix.as_bytes())
}

/// Quarantine scan prefix: `\x00QUAR:`
///
/// Scan all quarantined assertions (time-ordered).
pub fn quarantine_scan_prefix() -> Vec<u8> {
    global_key(b"QUAR:", b"")
}

/// Quarantine hash index key: `\x00QUAR_IDX:{hash_hex}`
///
/// Secondary index mapping hash → timestamp for O(1) hash-to-key resolution.
/// Without this index, finding a quarantine event by hash requires scanning
/// all entries since the primary key has timestamp as prefix.
pub fn quarantine_hash_index_key(hash_hex: &str) -> Vec<u8> {
    global_key(b"QUAR_IDX:", hash_hex.as_bytes())
}
