//! Flow registry - SINGLE source of truth with build-time discovery
//!
//! Flows are automatically discovered from FFI at build time.
//! The FLOW_DEFINITIONS and all probe functions are generated by build.rs.

// Include the generated probe functions
include!(concat!(env!("OUT_DIR"), "/flow_runners_generated.rs"));

/// Re-exports of all probe functions and types from the generated module
pub use generated::*;

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::Arc;

use common_utils::metadata::MaskedMetadata;
use domain_types::{connector_types::ConnectorEnum, router_data::ConnectorSpecificConfig};
use grpc_api_types::payments::PaymentMethod;

use crate::types::FlowResult;

/// Probes a flow by its definition.
/// This is the SINGLE function that dispatches to all flow runners.
pub fn probe_flow_by_definition(
    def: &FlowDefinition,
    connector: &ConnectorEnum,
    config: &Arc<ucs_env::configs::Config>,
    auth: ConnectorSpecificConfig,
    metadata: &MaskedMetadata,
    pm_variants: &HashMap<String, fn() -> PaymentMethod>,
) -> Option<BTreeMap<String, FlowResult>> {
    if def.has_payment_methods {
        // Authorize flow - iterate over payment methods
        let mut results = BTreeMap::new();
        for (pm_name, pm_fn) in pm_variants {
            let auth = crate::auth::dummy_auth(connector);
            let result = probe_authorize(connector, pm_name, pm_fn(), config, auth, metadata);
            results.insert(pm_name.clone(), result);
        }
        Some(results)
    } else {
        // Single flow - use dispatcher
        dispatch_probe(def.key, connector, config, auth, metadata).map(|result| {
            let mut m = BTreeMap::new();
            m.insert("default".to_string(), result);
            m
        })
    }
}
