//! Pattern learning configuration.

use std::path::PathBuf;

use serde::Deserialize;

/// Pattern learning configuration.
///
/// When LLM extraction discovers patterns that regex extractors miss,
/// these settings control whether and how patterns are recorded for
/// potential promotion to declarative extractors.
///
/// # Example
///
/// ```toml
/// [learning]
/// enabled = true
/// store = "local"
/// min_confidence = 0.7
/// prune_after_days = 90
/// max_patterns = 10000
///
/// [learning.promotion]
/// min_projects = 5
/// min_confidence = 0.8
/// auto_promote = false
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct LearningConfig {
    /// Enable pattern learning (default: false).
    ///
    /// When enabled, LLM-extracted claims are recorded as learned patterns
    /// for potential promotion to declarative extractors.
    pub enabled: bool,

    /// Storage backend for learned patterns.
    ///
    /// - `"local"`: Store in `~/.aphoria/learning/patterns.json`
    /// - `"hosted"`: Sync to hosted StemeDB server (future)
    pub store: String,

    /// Minimum LLM confidence to record a pattern (default: 0.7).
    ///
    /// Claims below this threshold are not recorded as patterns.
    pub min_confidence: f32,

    /// Days after which unused patterns are pruned (default: 90).
    ///
    /// Patterns not seen in this many days are removed during pruning.
    /// Promoted patterns are never pruned.
    pub prune_after_days: u32,

    /// Maximum number of patterns to store (default: 10000).
    ///
    /// When this limit is reached, the oldest non-promoted pattern is
    /// removed before adding a new one. This prevents unbounded growth
    /// of the pattern store.
    pub max_patterns: usize,

    /// Settings for pattern promotion to declarative extractors.
    pub promotion: PromotionConfig,
}

/// Configuration for promoting learned patterns to declarative extractors.
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct PromotionConfig {
    /// Minimum number of projects before a pattern can be promoted (default: 5).
    ///
    /// Patterns must be observed in at least this many distinct projects
    /// to be considered for promotion.
    pub min_projects: usize,

    /// Minimum average confidence for promotion (default: 0.8).
    ///
    /// The average LLM confidence across all observations must meet
    /// this threshold for promotion eligibility.
    pub min_confidence: f32,

    /// Automatically promote patterns that meet thresholds (default: false).
    ///
    /// When false, patterns meeting criteria are flagged for human review.
    /// When true, patterns are automatically promoted (Phase 9 feature).
    pub auto_promote: bool,

    /// Output directory for promoted YAML extractors.
    ///
    /// Default: `.aphoria/extractors/learned/`
    pub output_dir: PathBuf,

    /// Always require human review before promotion (default: true).
    ///
    /// Even if `auto_promote` is true, this flag can enforce review.
    pub require_review: bool,
}
