//! Curated Trust Pack registry.
//!
//! Provides a hardcoded registry of curated Trust Pack names and their URLs.
//! When hosting infrastructure is ready, these URLs will point to a real
//! pack distribution service.

use crate::AphoriaError;

/// A curated Trust Pack entry in the registry.
#[derive(Debug, Clone)]
pub struct TrustPackEntry {
    /// Short name used for `aphoria trust-pack install <name>`.
    pub name: String,
    /// Human-readable description.
    pub description: String,
    /// Download URL (placeholder until hosting is ready).
    pub url: String,
    /// What tier the pack's assertions target.
    pub tier: &'static str,
}

/// Built-in registry of curated Trust Packs.
static CURATED_PACKS: &[(&str, &str, &str, &str)] = &[
    (
        "security-hardening",
        "Hardcoded + vendor security assertions for common frameworks",
        "https://packs.aphoria.dev/security-hardening/latest.pack",
        "Tier 0-2",
    ),
    (
        "rfc-compliance",
        "All RFC normative statements (JWT, OAuth, TLS, HTTP)",
        "https://packs.aphoria.dev/rfc-compliance/latest.pack",
        "Tier 0",
    ),
    (
        "owasp-top10",
        "OWASP Cheat Sheet recommendations with CWE references",
        "https://packs.aphoria.dev/owasp-top10/latest.pack",
        "Tier 1",
    ),
];

/// Look up a curated pack by name.
pub fn lookup(name: &str) -> Result<TrustPackEntry, AphoriaError> {
    for &(pack_name, description, url, tier) in CURATED_PACKS {
        if pack_name == name {
            return Ok(TrustPackEntry {
                name: pack_name.to_string(),
                description: description.to_string(),
                url: url.to_string(),
                tier,
            });
        }
    }

    Err(AphoriaError::Config(format!(
        "Unknown trust pack '{}'. Available packs: {}",
        name,
        CURATED_PACKS.iter().map(|(n, _, _, _)| *n).collect::<Vec<_>>().join(", ")
    )))
}

/// List all available curated packs.
pub fn list_packs() -> Vec<TrustPackEntry> {
    CURATED_PACKS
        .iter()
        .map(|&(name, description, url, tier)| TrustPackEntry {
            name: name.to_string(),
            description: description.to_string(),
            url: url.to_string(),
            tier,
        })
        .collect()
}

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

    #[test]
    fn test_registry_lookup_exists() {
        let entry = lookup("security-hardening").expect("should find pack");
        assert_eq!(entry.name, "security-hardening");
        assert!(entry.url.contains("security-hardening"));
    }

    #[test]
    fn test_registry_lookup_not_found() {
        let result = lookup("nonexistent-pack");
        assert!(result.is_err());
    }

    #[test]
    fn test_list_packs() {
        let packs = list_packs();
        assert_eq!(packs.len(), 3);
        let names: Vec<&str> = packs.iter().map(|p| p.name.as_str()).collect();
        assert!(names.contains(&"security-hardening"));
        assert!(names.contains(&"rfc-compliance"));
        assert!(names.contains(&"owasp-top10"));
    }
}
