//! Core configuration types for Aphoria.

use std::path::PathBuf;

use serde::Deserialize;

use super::autonomous::AutonomousConfig;
use super::cross_project::CrossProjectConfig;
use super::eval::EvalConfig;
use super::extractors::ExtractorConfig;
use super::governance::GovernanceConfig;
use super::hosted::HostedConfig;
use super::learning::LearningConfig;
use super::llm::LlmConfig;
use super::predicates::PredicateAliasConfig;
use super::scan::{AliasConfig, CorpusConfig, ScanConfig};
use super::shadow::ShadowConfig;
use super::trust_pack::TrustPackConfig;
use super::CommunityConfig;

use crate::scope::ScopeConfig;

/// Default LLM model for extraction.
///
/// This is the single source of truth for the default model.
/// Change this constant to update the default across the codebase.
pub const DEFAULT_LLM_MODEL: &str = "gemini-3-flash-preview";

/// Top-level Aphoria configuration.
///
/// Loaded from `aphoria.toml` at the project root.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
pub struct AphoriaConfig {
    /// Project settings.
    pub project: ProjectConfig,

    /// Episteme instance settings.
    pub episteme: EpistemeConfig,

    /// Conflict threshold settings.
    pub thresholds: ThresholdConfig,

    /// Extractor settings.
    pub extractors: ExtractorConfig,

    /// Scan settings.
    pub scan: ScanConfig,

    /// Alias suggestion settings.
    pub aliases: AliasConfig,

    /// Corpus builder settings.
    pub corpus: CorpusConfig,

    /// Policy pack URIs to load.
    ///
    /// Supports:
    /// - Local paths: `file://./policies/security.pack` or `./policies/security.pack`
    /// - HTTP(S): `https://example.com/policies/security.pack`
    pub policies: Vec<String>,

    /// Hosted mode settings for team aggregation.
    pub hosted: HostedConfig,

    /// Community sharing settings for anonymous pattern contribution.
    pub community: CommunityConfig,

    /// LLM extraction settings for semantic claim detection.
    pub llm: LlmConfig,

    /// Pattern learning settings for LLM-discovered patterns.
    pub learning: LearningConfig,

    /// Predicate alias settings for semantic matching.
    pub predicate_aliases: PredicateAliasConfig,

    /// LLM evaluation settings for prompt optimization.
    pub eval: EvalConfig,

    /// Autonomous promotion settings for high-confidence patterns.
    pub autonomous: AutonomousConfig,

    /// Shadow mode testing settings for auto-promoted extractors.
    pub shadow: ShadowConfig,

    /// Cross-project learning settings for pattern sharing.
    pub cross_project: CrossProjectConfig,

    /// Trust Pack export settings.
    pub trust_pack: TrustPackConfig,

    /// Scope hierarchy settings for pattern inheritance.
    pub scope: ScopeConfig,

    /// Governance workflow settings for pattern promotion approval.
    pub governance: GovernanceConfig,
}

/// Project identification settings.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
pub struct ProjectConfig {
    /// Project name (auto-detected if not specified).
    pub name: Option<String>,

    /// Primary language (auto-detected if not specified).
    pub language: Option<String>,
}

/// Episteme instance configuration.
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct EpistemeConfig {
    /// Path to local Episteme data directory.
    pub data_dir: PathBuf,

    /// Remote Episteme URL (future feature).
    pub url: Option<String>,
}

/// Conflict threshold configuration.
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct ThresholdConfig {
    /// Conflict score at or above which to BLOCK.
    pub block: f32,

    /// Conflict score at or above which to FLAG.
    pub flag: f32,
}
