//! Hydrant ATProto indexer setup and configuration.
//!
//! Initializes Hydrant from environment and applies a signal-mode filter so only
//! repos that publish `space.polymodel.*` records are indexed. The returned
//! handle is cheaply cloneable; the firehose/backfill driver is started later via
//! [`Hydrant::run`] in [`super::start_indexing`].

use hydrant::FilterMode;
use hydrant::control::Hydrant;

/// NSID patterns to index: the whole Polymodel namespace, used for both
/// signal-mode discovery and the collection allowlist.
const COLLECTIONS: [&str; 1] = ["space.polymodel.*"];

/// Initialize Hydrant from environment and configure signal-mode filtering.
///
/// Does NOT start the firehose. The caller drives [`Hydrant::run`] (started in
/// [`super::start_indexing`]) to begin ingest and backfill.
pub async fn init_hydrant() -> anyhow::Result<Hydrant> {
    let hydrant = Hydrant::from_env()
        .await
        .map_err(|e| anyhow::anyhow!("hydrant init failed: {e}"))?;

    // Signal mode: only index repos that emit space.polymodel.* records, and
    // only store those collections. Code takes precedence over HYDRANT_FILTER_*
    // env vars, but the env vars are still documented in .env.example.
    hydrant
        .filter
        .set_mode(FilterMode::Filter)
        .set_signals(COLLECTIONS)
        .set_collections(COLLECTIONS)
        .apply()
        .await
        .map_err(|e| anyhow::anyhow!("hydrant filter apply failed: {e}"))?;

    tracing::info!("hydrant initialized with signal filter for space.polymodel.*");
    Ok(hydrant)
}
