//! Predicate Alias Keys — global predicate alias storage.
//!
//! Predicate aliases are stored globally (not per-subject) because they apply
//! across all assertions. A single alias set (e.g., `enabled ↔ required`) should
//! match assertions regardless of their subject.
//!
//! # Storage Layout
//!
//! | Key Pattern | Value | Purpose |
//! |-------------|-------|---------|
//! | `\x00PALS:{canonical}` | JSON-serialized `StoredPredicateAliasSet` | Alias set by canonical name |
//!
//! Global keys (prefixed with `\x00`) sort before all subject-prefixed keys.

use super::builders::global_key;

/// Predicate alias key: `\x00PALS:{canonical}`
///
/// Stores a predicate alias set keyed by its canonical predicate name.
/// The value is a JSON-serialized `StoredPredicateAliasSet`.
///
/// # Example
///
/// ```ignore
/// let key = predicate_alias_key("enabled");
/// // key = b"\x00PALS:enabled"
/// ```
pub fn predicate_alias_key(canonical: &str) -> Vec<u8> {
    global_key(b"PALS:", canonical.as_bytes())
}

/// Predicate alias scan prefix: `\x00PALS:`
///
/// Used to iterate over all stored predicate alias sets.
///
/// # Example
///
/// ```ignore
/// let prefix = predicate_alias_scan_prefix();
/// for (key, value) in store.scan_prefix(&prefix).await? {
///     // key = b"\x00PALS:{canonical}"
///     // value = JSON-serialized StoredPredicateAliasSet
/// }
/// ```
pub fn predicate_alias_scan_prefix() -> Vec<u8> {
    global_key(b"PALS:", b"")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_predicate_alias_key() {
        let key = predicate_alias_key("enabled");
        // Should be: \x00PALS:enabled
        assert_eq!(key[0], 0x00);
        assert!(key.starts_with(b"\x00PALS:"));
        assert!(key.ends_with(b"enabled"));
    }

    #[test]
    fn test_predicate_alias_scan_prefix() {
        let prefix = predicate_alias_scan_prefix();
        assert_eq!(prefix, b"\x00PALS:");
    }

    #[test]
    fn test_key_sorts_before_subjects() {
        let alias_key = predicate_alias_key("enabled");
        let subject_key = b"rfc://5246/tls\x00H:abc123";

        // Global keys (starting with \x00) sort before subject keys
        assert!(alias_key.as_slice() < subject_key.as_slice());
    }
}
