//! Source classification and authority tiers.

use rkyv::{Archive, Deserialize, Serialize};

/// Classification of the source's authority and reliability tier.
///
/// Sources are tiered from most authoritative (Regulatory) to least (Anecdotal).
/// This enables:
/// - **Indexing**: Query "show me only regulatory sources"
/// - **Decay rates**: Anecdotal claims decay faster than clinical evidence
/// - **Trust weighting**: Tier 0 sources carry more weight in conflict resolution
///
/// # Consumer Health Tiers
///
/// | Tier | Class | Example |
/// |------|-------|---------|
/// | 0 | Regulatory | FDA approval letters, EMA assessments |
/// | 1 | Clinical | Phase III trials, peer-reviewed RCTs |
/// | 2 | Observational | Real-world evidence, cohort studies |
/// | 3 | Expert | Medical professional opinions, guidelines |
/// | 4 | Community | Curated forums, patient advocacy groups |
/// | 5 | Anecdotal | Reddit posts, individual testimonials |
#[derive(Archive, Deserialize, Serialize, Debug, Clone, Copy, PartialEq, Eq, Default)]
#[archive(check_bytes)]
pub enum SourceClass {
    /// Tier 0: Regulatory bodies (FDA, EMA, WHO).
    /// Highest authority. Assertions rarely decay.
    Regulatory,
    /// Tier 1: Clinical trials, peer-reviewed research.
    /// High authority. Slow decay unless superseded by new studies.
    Clinical,
    /// Tier 2: Observational studies, real-world evidence.
    /// Medium-high authority. Moderate decay.
    Observational,
    /// Tier 3: Expert opinions, medical guidelines.
    /// Medium authority. Faster decay as guidelines update.
    #[default]
    Expert,
    /// Tier 4: Curated community knowledge.
    /// Medium-low authority. Community-moderated content.
    Community,
    /// Tier 5: Individual reports, testimonials, social media.
    /// Lowest authority. Fast decay, high volume.
    Anecdotal,
}

impl SourceClass {
    /// Returns the tier number (0-5) for this source class.
    pub fn tier(&self) -> u8 {
        match self {
            SourceClass::Regulatory => 0,
            SourceClass::Clinical => 1,
            SourceClass::Observational => 2,
            SourceClass::Expert => 3,
            SourceClass::Community => 4,
            SourceClass::Anecdotal => 5,
        }
    }

    /// Returns the default decay half-life in days for this source class.
    ///
    /// Higher tiers decay faster. Regulatory sources essentially never decay,
    /// while anecdotal claims have a 30-day half-life.
    pub fn default_decay_days(&self) -> Option<u32> {
        match self {
            SourceClass::Regulatory => None,         // Never decays
            SourceClass::Clinical => Some(730),      // 2 years
            SourceClass::Observational => Some(365), // 1 year
            SourceClass::Expert => Some(180),        // 6 months
            SourceClass::Community => Some(90),      // 3 months
            SourceClass::Anecdotal => Some(30),      // 1 month
        }
    }

    /// Returns a weight multiplier for conflict resolution.
    ///
    /// Used by lenses to weight sources by authority tier.
    pub fn authority_weight(&self) -> f32 {
        match self {
            SourceClass::Regulatory => 1.0,
            SourceClass::Clinical => 0.9,
            SourceClass::Observational => 0.7,
            SourceClass::Expert => 0.5,
            SourceClass::Community => 0.3,
            SourceClass::Anecdotal => 0.1,
        }
    }
}
