//! Bridge between KOGRAL events and the stratum-orchestrator pipeline runtime
//!
//! Maps `KogralEvent` subjects and payloads into `PipelineContext` instances
//! that the stratum-orchestrator `StageRunner` can execute.

use std::{path::PathBuf, sync::Arc};

use stratum_orchestrator::context::PipelineContext;
use stratum_state::StateTracker;

use crate::events::KogralEvent;

/// Construct a `PipelineContext` from a `KogralEvent`.
///
/// The event's subject becomes the pipeline trigger subject and `payload`
/// is passed through as the trigger payload. A fresh `PipelineRun` record
/// is written to `state` before this function returns.
///
/// # Errors
///
/// Propagates any error from `PipelineContext::new` (DB write failure).
pub async fn pipeline_context_from_event(
    event: &KogralEvent,
    payload: serde_json::Value,
    state: Arc<dyn StateTracker>,
    schema_dir: PathBuf,
) -> anyhow::Result<PipelineContext> {
    PipelineContext::new(event.subject(), payload, state, schema_dir).await
}
