//! Hosted mode configuration types.

use serde::Deserialize;

/// Hosted mode configuration for team aggregation.
///
/// When `url` is set, Aphoria operates in "hosted mode" where all observations
/// are automatically synced to a central StemeDB server. This enables teams to
/// aggregate patterns across all projects.
///
/// # Example
///
/// ```toml
/// [hosted]
/// url = "https://episteme.acme.corp"
/// project_id = "billing-service"
/// team_id = "platform-team"
/// sync_mode = "remote-only"
/// offline_fallback = "skip"
/// api_key_env = "APHORIA_API_KEY"
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct HostedConfig {
    /// URL of the team's StemeDB server.
    ///
    /// When set, enables hosted mode with automatic sync.
    /// Example: `https://episteme.acme.corp`
    pub url: Option<String>,

    /// Project identifier for this codebase.
    ///
    /// If not set, defaults to `[project.name]` from the config.
    pub project_id: Option<String>,

    /// Team identifier for multi-team servers.
    ///
    /// Optional, helps with data segregation on shared servers.
    pub team_id: Option<String>,

    /// How to sync observations.
    ///
    /// - `remote-only`: Only push to remote server (no local storage)
    /// - `local-and-remote`: Store locally AND push to remote
    pub sync_mode: SyncMode,

    /// Behavior when the server is unreachable.
    ///
    /// - `skip`: Continue without syncing (default, doesn't block developers)
    /// - `fail`: Fail the scan if sync fails
    /// - `queue`: Queue for later sync (not yet implemented)
    pub offline_fallback: OfflineFallback,

    /// Maximum number of retry attempts for HTTP requests.
    pub max_retries: u32,

    /// Delay between retry attempts in milliseconds.
    pub retry_delay_ms: u64,

    /// Name of the environment variable containing the API key.
    ///
    /// If set and the env var exists, adds `Authorization: Bearer <key>` header.
    pub api_key_env: String,
}

impl HostedConfig {
    /// Returns true if hosted mode is enabled (URL is set).
    pub fn is_enabled(&self) -> bool {
        self.url.is_some()
    }
}

/// How to sync observations in hosted mode.
#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum SyncMode {
    /// Only push to remote server (no local storage).
    ///
    /// This is the default to avoid duplicate storage.
    #[default]
    RemoteOnly,

    /// Store locally AND push to remote.
    ///
    /// Use this for development or when you need local history.
    LocalAndRemote,
}

/// Behavior when the hosted server is unreachable.
#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum OfflineFallback {
    /// Continue without syncing (doesn't block developers).
    #[default]
    Skip,

    /// Fail the scan if sync fails.
    ///
    /// Use this when sync is mandatory (e.g., CI/CD pipelines).
    Fail,

    /// Queue for later sync (not yet implemented).
    Queue,
}
