//! Global keys — metadata, trust, quotas, epochs (sort first under `\x00`).
//!
//! Format: `\x00{TAG}:{suffix}`

use super::builders::global_key;

/// Trust rank key: `\x00TRUST:{agent_id_hex}`
pub fn trust_rank_key(agent_id_hex: &str) -> Vec<u8> {
    global_key(b"TRUST:", agent_id_hex.as_bytes())
}

/// Quota record key: `\x00QUOTA:{agent_hex}:{window}`
pub fn quota_key(agent_hex: &str, window: u64) -> Vec<u8> {
    let suffix = format!("{}:{}", agent_hex, window);
    global_key(b"QUOTA:", suffix.as_bytes())
}

/// Quota limit key: `\x00QLIMIT:{agent_id_hex}`
pub fn quota_limit_key(agent_id_hex: &str) -> Vec<u8> {
    global_key(b"QLIMIT:", agent_id_hex.as_bytes())
}

/// Epoch key: `\x00E:{epoch_id_hex}`
pub fn epoch_key(epoch_id_hex: &str) -> Vec<u8> {
    global_key(b"E:", epoch_id_hex.as_bytes())
}

/// Superseded marker key: `\x00SUPERSEDED:{epoch_id_hex}`
pub fn superseded_key(epoch_id_hex: &str) -> Vec<u8> {
    global_key(b"SUPERSEDED:", epoch_id_hex.as_bytes())
}

/// Supersession record key: `\x00SUP:{target_hash_hex}`
pub fn supersession_key(target_hash_hex: &str) -> Vec<u8> {
    global_key(b"SUP:", target_hash_hex.as_bytes())
}

/// Supersession agent index key: `\x00SUP:IDX:{agent_hex}:{ts_be_bytes}`
pub fn supersession_index_key(agent_hex: &str, timestamp_be_bytes: &[u8]) -> Vec<u8> {
    let mut suffix = Vec::with_capacity(agent_hex.len() + 1 + timestamp_be_bytes.len());
    suffix.extend_from_slice(agent_hex.as_bytes());
    suffix.push(b':');
    suffix.extend_from_slice(timestamp_be_bytes);
    global_key(b"SUP:IDX:", &suffix)
}

/// Supersession agent scan prefix: `\x00SUP:IDX:{agent_hex}:`
pub fn supersession_index_prefix(agent_hex: &str) -> Vec<u8> {
    let suffix = format!("{}:", agent_hex);
    global_key(b"SUP:IDX:", suffix.as_bytes())
}

/// Audit record key: `\x00AUD:{query_id_hex}`
pub fn audit_key(query_id_hex: &str) -> Vec<u8> {
    global_key(b"AUD:", query_id_hex.as_bytes())
}

/// Audit agent index key: `\x00AUDA:{agent_hex}:{timestamp_hex}:{query_hex}`
pub fn audit_agent_index_key(agent_hex: &str, timestamp_hex: &str, query_hex: &str) -> Vec<u8> {
    let suffix = format!("{}:{}:{}", agent_hex, timestamp_hex, query_hex);
    global_key(b"AUDA:", suffix.as_bytes())
}

/// Audit agent scan prefix: `\x00AUDA:{agent_hex}:`
pub fn audit_agent_prefix(agent_hex: &str) -> Vec<u8> {
    let suffix = format!("{}:", agent_hex);
    global_key(b"AUDA:", suffix.as_bytes())
}

/// Audit listing prefix: `\x00AUD:`
pub fn audit_scan_prefix() -> Vec<u8> {
    global_key(b"AUD:", b"")
}

/// Escalation key: `\x00ESC:{timestamp}:{id_hex}`
pub fn escalation_key(timestamp: u64, id_hex: &str) -> Vec<u8> {
    let suffix = format!("{}:{}", timestamp, id_hex);
    global_key(b"ESC:", suffix.as_bytes())
}

/// Escalation scan prefix: `\x00ESC:`
pub fn escalation_scan_prefix() -> Vec<u8> {
    global_key(b"ESC:", b"")
}

/// Trust pack key: `\x00TP:{pack_id_bytes}`
pub fn trust_pack_key(pack_id: &[u8]) -> Vec<u8> {
    global_key(b"TP:", pack_id)
}

/// Trust pack scan prefix: `\x00TP:`
pub fn trust_pack_scan_prefix() -> Vec<u8> {
    global_key(b"TP:", b"")
}

/// Gold standard verified key: `\x00GS_VERIFIED:{agent_hex}:{subject}:{predicate}`
pub fn gs_verified_key(agent_hex: &str, subject: &str, predicate: &str) -> Vec<u8> {
    let suffix = format!("{}:{}:{}", agent_hex, subject, predicate);
    global_key(b"GS_VERIFIED:", suffix.as_bytes())
}

/// Cursor key: `\x00META:cursor:ingest`
pub fn cursor_key() -> Vec<u8> {
    global_key(b"META:cursor:ingest", b"")
}

/// Assertion count key: `\x00META:assertion_count`
pub fn assertion_count_key() -> Vec<u8> {
    global_key(b"META:assertion_count", b"")
}

/// Trust rank scan prefix for decay: `\x00TRUST:`
pub fn trust_rank_scan_prefix() -> Vec<u8> {
    global_key(b"TRUST:", b"")
}
