Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

118 KB
Raw Blame
use std::{
    collections::{HashMap, HashSet},
    str::FromStr,
    sync::LazyLock,
};

use api_models::{
    open_router as or_types,
    routing::{
        self as api_routing, ComparisonType, ConnectorSelection, ConnectorVolumeSplit,
        DeRoutableConnectorChoice, MetadataValue, NumberComparison, RoutableConnectorChoice,
        RoutingEvaluateRequest, RoutingEvaluateResponse, ValueType,
    },
};
use async_trait::async_trait;
use common_enums::TransactionType;
use common_utils::{ext_traits::BytesExt, id_type, types::MinorUnit};
use diesel_models::{enums, routing_algorithm};
use error_stack::ResultExt;
use euclid::{
    backend::BackendInput,
    enums::RoutableConnectors,
    frontend::{
        ast::{self},
        dir::{self, transformers::IntoDirValue},
    },
};
#[cfg(all(feature = "v1", feature = "dynamic_routing"))]
use external_services::grpc_client::dynamic_routing as ir_client;
use hyperswitch_domain_models::business_profile;
use hyperswitch_interfaces::events::routing_api_logs as routing_events;
use hyperswitch_masking::{Mask, PeekInterface};
use router_env::RequestId;
use serde::{Deserialize, Serialize};

use super::RoutingResult;
use crate::{
    core::{configs::dimension_state, errors, metrics},
    db::domain,
    routes::{app::SessionStateInfo, SessionState},
    services::{self, logger},
    types::transformers::ForeignInto,
};

// New Trait for handling Euclid API calls
#[async_trait]
pub trait DecisionEngineApiHandler {
    async fn send_decision_engine_request<Req, Res>(
        state: &SessionState,
        http_method: services::Method,
        path: &str,
        request_body: Option<Req>, // Option to handle GET/DELETE requests without body
        timeout: Option<u64>,
        events_wrapper: Option<RoutingEventsWrapper<Req>>,
    ) -> RoutingResult<RoutingEventsResponse<Res>>
    where
        Req: Serialize + Send + Sync + 'static + Clone,
        Res: Serialize + serde::de::DeserializeOwned + Send + 'static + std::fmt::Debug + Clone;
}

// Struct to implement the DecisionEngineApiHandler trait
pub struct EuclidApiClient;

pub struct ConfigApiClient;

pub struct SRApiClient;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HybridRoutingRequest {
    pub static_routing_request: Option<RoutingEvaluateRequest>,
    pub dynamic_routing_request: Option<or_types::OpenRouterDecideGatewayRequest>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HybridRoutingResponse {
    pub static_routing: Option<RoutingEvaluateResponse>,
    pub dynamic_routing: Option<DynamicRoutingWrapper>,
    pub evaluated_connectors: Option<Vec<DeRoutableConnectorChoice>>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DynamicRoutingWrapper {
    pub status: String,
    pub decision: Option<or_types::DecideGatewayResponse>,
    pub fallback_connectors: Option<Vec<DeRoutableConnectorChoice>>,
}

#[derive(Clone, Debug)]
pub struct HybridRoutingOutcome {
    pub connectors: Vec<RoutableConnectorChoice>,
    pub routing_approach: RoutingApproach,
}

impl HybridRoutingOutcome {
    /// Empty outcome (no DE connectors); the caller falls back to the Hyperswitch static result.
    pub fn empty() -> Self {
        Self {
            connectors: Vec::new(),
            routing_approach: RoutingApproach::Default,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DecisionEngineEndpoint {
    DecideGateway,
    UpdateGatewayScore,
    RoutingEvaluate,
    RoutingEvaluateBatch,
    RoutingHybrid,
    RoutingCreate,
    RoutingActivate,
    RoutingListActive,
    RoutingList,
    RuleConfig,
    Other,
}

impl DecisionEngineEndpoint {
    const DECIDE_GATEWAY_PATH: &'static str = "decide-gateway";
    const UPDATE_GATEWAY_SCORE_PATH: &'static str = "update-gateway-score";
    const ROUTING_EVALUATE_PATH: &'static str = "routing/evaluate";
    const ROUTING_EVALUATE_BATCH_PATH: &'static str = "routing/evaluate/batch";
    const ROUTING_HYBRID_PATH: &'static str = "routing/hybrid";
    const ROUTING_CREATE_PATH: &'static str = "routing/create";
    const ROUTING_ACTIVATE_PATH: &'static str = "routing/activate";
    const ROUTING_LIST_ACTIVE_PATH: &'static str = "routing/list/active";
    const ROUTING_LIST_PATH: &'static str = "routing/list";
    const RULE_PATH: &'static str = "rule";

    const DECIDE_GATEWAY_LABEL: &'static str = "decide_gateway";
    const UPDATE_GATEWAY_SCORE_LABEL: &'static str = "update_gateway_score";
    const ROUTING_EVALUATE_LABEL: &'static str = "routing_evaluate";
    const ROUTING_EVALUATE_BATCH_LABEL: &'static str = "routing_evaluate_batch";
    const ROUTING_HYBRID_LABEL: &'static str = "routing_hybrid";
    const ROUTING_CREATE_LABEL: &'static str = "routing_create";
    const ROUTING_ACTIVATE_LABEL: &'static str = "routing_activate";
    const ROUTING_LIST_ACTIVE_LABEL: &'static str = "routing_list_active";
    const ROUTING_LIST_LABEL: &'static str = "routing_list";
    const RULE_CONFIG_LABEL: &'static str = "rule_config";
    const OTHER_LABEL: &'static str = "other";

    fn from_path(path: &str) -> Self {
        let path = path.split('?').next().unwrap_or(path).trim_matches('/');
        match path {
            Self::DECIDE_GATEWAY_PATH => Self::DecideGateway,
            Self::UPDATE_GATEWAY_SCORE_PATH => Self::UpdateGatewayScore,
            Self::ROUTING_EVALUATE_BATCH_PATH => Self::RoutingEvaluateBatch,
            Self::ROUTING_EVALUATE_PATH => Self::RoutingEvaluate,
            Self::ROUTING_HYBRID_PATH => Self::RoutingHybrid,
            Self::ROUTING_CREATE_PATH => Self::RoutingCreate,
            Self::ROUTING_ACTIVATE_PATH => Self::RoutingActivate,
            p if p.starts_with(Self::ROUTING_LIST_ACTIVE_PATH) => Self::RoutingListActive,
            p if p.starts_with(Self::ROUTING_LIST_PATH) => Self::RoutingList,
            p if p.starts_with(Self::RULE_PATH) => Self::RuleConfig,
            _ => Self::Other,
        }
    }

    fn as_label(self) -> &'static str {
        match self {
            Self::DecideGateway => Self::DECIDE_GATEWAY_LABEL,
            Self::UpdateGatewayScore => Self::UPDATE_GATEWAY_SCORE_LABEL,
            Self::RoutingEvaluate => Self::ROUTING_EVALUATE_LABEL,
            Self::RoutingEvaluateBatch => Self::ROUTING_EVALUATE_BATCH_LABEL,
            Self::RoutingHybrid => Self::ROUTING_HYBRID_LABEL,
            Self::RoutingCreate => Self::ROUTING_CREATE_LABEL,
            Self::RoutingActivate => Self::ROUTING_ACTIVATE_LABEL,
            Self::RoutingListActive => Self::ROUTING_LIST_ACTIVE_LABEL,
            Self::RoutingList => Self::ROUTING_LIST_LABEL,
            Self::RuleConfig => Self::RULE_CONFIG_LABEL,
            Self::Other => Self::OTHER_LABEL,
        }
    }
}

const DECISION_ENGINE_ADMIN_SECRET_HEADER: &str = "x-admin-secret";

pub async fn build_and_send_decision_engine_http_request<Req, Res, ErrRes>(
    state: &SessionState,
    http_method: services::Method,
    path: &str,
    request_body: Option<Req>,
    timeout: Option<u64>,
    context_message: &str,
    events_wrapper: Option<RoutingEventsWrapper<Req>>,
) -> RoutingResult<RoutingEventsResponse<Res>>
where
    Req: Serialize + Send + Sync + 'static + Clone,
    Res: Serialize + serde::de::DeserializeOwned + std::fmt::Debug + Clone + 'static,
    ErrRes: serde::de::DeserializeOwned + std::fmt::Debug + Clone + DecisionEngineErrorsInterface,
{
    let decision_engine_base_url = &state.conf.open_router.url;
    let url = format!("{decision_engine_base_url}/{path}");
    logger::debug!(decision_engine_api_call_url = %url, decision_engine_request_path = %path, http_method = ?http_method, "decision_engine: Initiating decision_engine API call ({})", context_message);

    let mut request_builder = services::RequestBuilder::new()
        .method(http_method)
        .url(&url);

    // The Decision Engine authenticates Hyperswitch via a shared admin secret: its admin
    // endpoints (merchant provisioning, SSO code mint) verify the header in the handler, and
    // its protected router accepts it as service-to-service auth. Attach it on every call
    // when configured; an empty secret omits the header.
    let admin_secret = &state.conf.open_router.admin_secret;
    if !admin_secret.peek().is_empty() {
        request_builder = request_builder.headers(vec![(
            DECISION_ENGINE_ADMIN_SECRET_HEADER.to_string(),
            admin_secret.clone().into_masked(),
        )]);
    }

    if let Some(body_content) = request_body {
        let body = common_utils::request::RequestContent::Json(Box::new(body_content));
        request_builder = request_builder.set_body(body);
    }

    let http_request = request_builder.build();
    let should_parse_response = events_wrapper
        .as_ref()
        .map(|wrapper| wrapper.parse_response)
        .unwrap_or(true);

    let endpoint_label = DecisionEngineEndpoint::from_path(path).as_label();

    let (merchant_id_label, profile_id_label) = events_wrapper
        .as_ref()
        .map(|wrapper| {
            (
                wrapper.merchant_id.get_string_repr().to_string(),
                wrapper.profile_id.get_string_repr().to_string(),
            )
        })
        .unwrap_or_else(|| ("unknown".to_string(), "unknown".to_string()));

    let closure = || async {
        let request_start = std::time::Instant::now();
        let response =
            services::call_connector_api(state, http_request, "Decision Engine API call", timeout)
                .await
                .change_context(errors::RoutingError::OpenRouterCallFailed)?;

        let status_code = match &response {
            Ok(res) => res.status_code,
            Err(err) => err.status_code,
        };
        let metrics_attributes = router_env::metric_attributes!(
            ("endpoint", endpoint_label),
            ("status_code", status_code.to_string()),
            ("merchant_id", merchant_id_label.clone()),
            ("profile_id", profile_id_label.clone()),
        );
        metrics::DECISION_ENGINE_REQUEST_TIME
            .record(request_start.elapsed().as_secs_f64(), metrics_attributes);
        metrics::DECISION_ENGINE_REQUESTS.add(1, metrics_attributes);

        match response {
            Ok(resp) => {
                let resp = should_parse_response
                    .then(|| {
                        if std::any::TypeId::of::<Res>() == std::any::TypeId::of::<String>()
                            && resp.response.is_empty()
                        {
                            return serde_json::from_str::<Res>("\"\"").change_context(
                                errors::RoutingError::OpenRouterError(
                                    "Failed to parse empty response as String".into(),
                                ),
                            );
                        }
                        let response_type: Res = resp
                            .response
                            .parse_struct(std::any::type_name::<Res>())
                            .change_context(errors::RoutingError::OpenRouterError(
                                "Failed to parse the response from open_router".into(),
                            ))?;

                        Ok::<_, error_stack::Report<errors::RoutingError>>(response_type)
                    })
                    .transpose()?;

                logger::debug!("decision_engine_success_response: {:?}", resp);

                Ok(resp)
            }
            Err(err) => {
                logger::debug!(
                    "decision_engine: Received response from Decision Engine API ({:?})",
                    String::from_utf8_lossy(&err.response) // For logging
                );

                let err_resp: ErrRes = err
                    .response
                    .parse_struct(std::any::type_name::<ErrRes>())
                    .change_context(errors::RoutingError::OpenRouterError(
                    "Failed to parse the response from open_router".into(),
                ))?;

                logger::error!(
                    decision_engine_error_code = %err_resp.get_error_code(),
                    decision_engine_error_message = %err_resp.get_error_message(),
                    decision_engine_raw_response = ?err_resp.get_error_data(),
                );

                Err(error_stack::report!(
                    errors::RoutingError::RoutingEventsError {
                        message: err_resp.get_error_message(),
                        status_code: err.status_code,
                    }
                ))
            }
        }
    };

    let events_response = if let Some(wrapper) = events_wrapper {
        wrapper
            .construct_event_builder(
                url,
                routing_events::RoutingEngine::DecisionEngine,
                routing_events::ApiMethod::Rest(http_method),
            )?
            .trigger_event(state, closure)
            .await?
    } else {
        // Keep the closure's error context (e.g. `RoutingEventsError` carrying the DE status
        // code) so callers can react to specific statuses, like the 404-gated merchant
        // provisioning in the SSO mint flow.
        let resp = closure().await?;

        RoutingEventsResponse::new(None, resp)
    };

    Ok(events_response)
}

#[async_trait]
impl DecisionEngineApiHandler for EuclidApiClient {
    async fn send_decision_engine_request<Req, Res>(
        state: &SessionState,
        http_method: services::Method,
        path: &str,
        request_body: Option<Req>, // Option to handle GET/DELETE requests without body
        timeout: Option<u64>,
        events_wrapper: Option<RoutingEventsWrapper<Req>>,
    ) -> RoutingResult<RoutingEventsResponse<Res>>
    where
        Req: Serialize + Send + Sync + 'static + Clone,
        Res: Serialize + serde::de::DeserializeOwned + Send + 'static + std::fmt::Debug + Clone,
    {
        let event_response = build_and_send_decision_engine_http_request::<_, _, DeErrorResponse>(
            state,
            http_method,
            path,
            request_body,
            timeout,
            "parsing response",
            events_wrapper,
        )
        .await?;

        // Reject an empty DE response even though the parsed value itself is unused here.
        event_response
            .response
            .as_ref()
            .ok_or(errors::RoutingError::OpenRouterError(
                "Response from decision engine API is empty".to_string(),
            ))?;

        Ok(event_response)
    }
}

#[async_trait]
impl DecisionEngineApiHandler for ConfigApiClient {
    async fn send_decision_engine_request<Req, Res>(
        state: &SessionState,
        http_method: services::Method,
        path: &str,
        request_body: Option<Req>,
        timeout: Option<u64>,
        events_wrapper: Option<RoutingEventsWrapper<Req>>,
    ) -> RoutingResult<RoutingEventsResponse<Res>>
    where
        Req: Serialize + Send + Sync + 'static + Clone,
        Res: Serialize + serde::de::DeserializeOwned + Send + 'static + std::fmt::Debug + Clone,
    {
        let events_response = build_and_send_decision_engine_http_request::<_, _, DeErrorResponse>(
            state,
            http_method,
            path,
            request_body,
            timeout,
            "parsing response",
            events_wrapper,
        )
        .await?;

        let parsed_response =
            events_response
                .response
                .as_ref()
                .ok_or(errors::RoutingError::OpenRouterError(
                    "Response from decision engine API is empty".to_string(),
                ))?;
        logger::debug!(parsed_response = ?parsed_response, response_type = %std::any::type_name::<Res>(), decision_engine_request_path = %path, "decision_engine_config: Successfully parsed response from Decision Engine config API");
        Ok(events_response)
    }
}

#[async_trait]
impl DecisionEngineApiHandler for SRApiClient {
    async fn send_decision_engine_request<Req, Res>(
        state: &SessionState,
        http_method: services::Method,
        path: &str,
        request_body: Option<Req>,
        timeout: Option<u64>,
        events_wrapper: Option<RoutingEventsWrapper<Req>>,
    ) -> RoutingResult<RoutingEventsResponse<Res>>
    where
        Req: Serialize + Send + Sync + 'static + Clone,
        Res: Serialize + serde::de::DeserializeOwned + Send + 'static + std::fmt::Debug + Clone,
    {
        let events_response =
            build_and_send_decision_engine_http_request::<_, _, or_types::ErrorResponse>(
                state,
                http_method,
                path,
                request_body,
                timeout,
                "parsing response",
                events_wrapper,
            )
            .await?;

        let parsed_response =
            events_response
                .response
                .as_ref()
                .ok_or(errors::RoutingError::OpenRouterError(
                    "Response from decision engine API is empty".to_string(),
                ))?;
        logger::debug!(parsed_response = ?parsed_response, response_type = %std::any::type_name::<Res>(), decision_engine_request_path = %path, "decision_engine_config: Successfully parsed response from Decision Engine config API");
        Ok(events_response)
    }
}

const EUCLID_API_TIMEOUT: u64 = 5;

fn convert_fallback_to_de_choices(
    fallback_output: Vec<RoutableConnectorChoice>,
) -> Vec<DeRoutableConnectorChoice> {
    fallback_output
        .into_iter()
        .map(|connector| DeRoutableConnectorChoice {
            gateway_name: connector.connector,
            gateway_id: connector.merchant_connector_id,
        })
        .collect()
}

pub fn build_static_routing_request_for_hybrid(
    created_by: String,
    payment_id: String,
    input: BackendInput,
    fallback_output: Vec<RoutableConnectorChoice>,
) -> RoutingResult<RoutingEvaluateRequest> {
    convert_backend_input_to_routing_eval(
        created_by,
        Some(payment_id),
        input,
        convert_fallback_to_de_choices(fallback_output),
        TransactionType::Payment,
    )
}

fn infer_hybrid_routing_approach(response: &HybridRoutingResponse) -> RoutingApproach {
    response
        .dynamic_routing
        .as_ref()
        .and_then(|dynamic_routing| dynamic_routing.decision.as_ref())
        .and_then(|decision| decision.routing_approach.as_deref())
        .map(RoutingApproach::from_decision_engine_approach)
        .unwrap_or_else(|| {
            if response.static_routing.is_some() {
                RoutingApproach::StaticRouting
            } else {
                RoutingApproach::Default
            }
        })
}

pub fn normalize_hybrid_routing_response(
    response: &HybridRoutingResponse,
) -> RoutingResult<HybridRoutingOutcome> {
    let outcome = if let Some(evaluated_connectors) = response
        .evaluated_connectors
        .as_ref()
        .filter(|connectors| !connectors.is_empty())
    {
        let connectors = evaluated_connectors
            .iter()
            .cloned()
            .map(RoutableConnectorChoice::from)
            .collect();

        HybridRoutingOutcome {
            connectors,
            routing_approach: infer_hybrid_routing_approach(response),
        }
    } else if let Some(static_routing_response) = response.static_routing.as_ref() {
        let static_output_connectors = extract_de_output_connectors(
            static_routing_response.output.clone(),
        )
        .map_err(|error| {
            logger::error!(
                error=?error,
                "euclid: failed to extract connector from hybrid static output"
            );
            error
        })?;

        let static_connectors = transform_de_output_for_router(
            static_output_connectors,
            static_routing_response.evaluated_output.clone(),
        )
        .map_err(|error| {
            logger::error!(
                error=?error,
                "euclid: failed to transform connectors from hybrid static output"
            );
            error
        })?;

        HybridRoutingOutcome {
            connectors: static_connectors,
            routing_approach: RoutingApproach::StaticRouting,
        }
    } else {
        logger::debug!(
            "euclid: hybrid routing response did not include usable evaluated connectors or static output; returning empty connector set"
        );
        HybridRoutingOutcome::empty()
    };

    Ok(outcome)
}

#[derive(Debug, Clone, serde::Deserialize)]
#[serde(untagged)]
enum HybridErrorResponse {
    Static(DeErrorResponse),
    Dynamic(or_types::ErrorResponse),
}

impl DecisionEngineErrorsInterface for HybridErrorResponse {
    fn get_error_message(&self) -> String {
        match self {
            Self::Static(error) => error.get_error_message(),
            Self::Dynamic(error) => error.get_error_message(),
        }
    }

    fn get_error_code(&self) -> String {
        match self {
            Self::Static(error) => error.get_error_code(),
            Self::Dynamic(error) => error.get_error_code(),
        }
    }

    fn get_error_data(&self) -> Option<String> {
        match self {
            Self::Static(error) => error.get_error_data(),
            Self::Dynamic(error) => error.get_error_data(),
        }
    }
}

pub async fn decision_engine_hybrid_routing(
    state: &SessionState,
    business_profile: &domain::Profile,
    payment_id: String,
    hybrid_request: HybridRoutingRequest,
    _fallback_connectors: Vec<RoutableConnectorChoice>,
) -> RoutingResult<HybridRoutingOutcome> {
    let routing_events_wrapper = RoutingEventsWrapper::new(
        state.tenant.tenant_id.clone(),
        state.request_id.clone(),
        payment_id,
        business_profile.get_id().to_owned(),
        business_profile.merchant_id.to_owned(),
        "DecisionEngine: Routing".to_string(),
        Some(hybrid_request.clone()),
        true,
        false,
    );

    let event_response = build_and_send_decision_engine_http_request::<_, _, HybridErrorResponse>(
        state,
        services::Method::Post,
        "routing/hybrid",
        Some(hybrid_request),
        Some(EUCLID_API_TIMEOUT),
        "parsing response",
        Some(routing_events_wrapper),
    )
    .await?;

    let hybrid_response: HybridRoutingResponse =
        event_response
            .response
            .ok_or(errors::RoutingError::OpenRouterError(
                "euclid: response from decision engine hybrid API is empty".to_string(),
            ))?;

    let outcome = normalize_hybrid_routing_response(&hybrid_response)?;
    let mut routing_event =
        event_response
            .event
            .ok_or(errors::RoutingError::RoutingEventsError {
                message: "euclid: routing event not found in hybrid events response".to_string(),
                status_code: 500,
            })?;

    routing_event.set_routing_approach(outcome.routing_approach.to_string());
    routing_event.set_routable_connectors(outcome.connectors.clone());
    state.event_handler().log_event(&routing_event);

    Ok(outcome)
}

pub async fn perform_decision_euclid_routing(
    state: &SessionState,
    input: BackendInput,
    created_by: String,
    payment_id: String,
    events_wrapper: RoutingEventsWrapper<RoutingEvaluateRequest>,
    fallback_output: Vec<RoutableConnectorChoice>,
    algorithm_for: TransactionType,
) -> RoutingResult<RoutingEvaluateResponse> {
    logger::debug!("decision_engine_euclid: evaluate api call for euclid routing evaluation");

    let mut events_wrapper = events_wrapper;
    let fallback_output = convert_fallback_to_de_choices(fallback_output);

    let routing_request = convert_backend_input_to_routing_eval(
        created_by,
        Some(payment_id),
        input,
        fallback_output,
        algorithm_for,
    )?;
    events_wrapper.set_request_body(routing_request.clone());

    let event_response = EuclidApiClient::send_decision_engine_request(
        state,
        services::Method::Post,
        "routing/evaluate",
        Some(routing_request),
        Some(EUCLID_API_TIMEOUT),
        Some(events_wrapper),
    )
    .await?;

    let euclid_response: RoutingEvaluateResponse =
        event_response
            .response
            .ok_or(errors::RoutingError::OpenRouterError(
                "Response from decision engine API is empty".to_string(),
            ))?;

    let mut routing_event =
        event_response
            .event
            .ok_or(errors::RoutingError::RoutingEventsError {
                message: "Routing event not found in EventsResponse".to_string(),
                status_code: 500,
            })?;

    routing_event.set_routing_approach(RoutingApproach::StaticRouting.to_string());
    routing_event.set_routable_connectors(euclid_response.evaluated_output.clone());
    state.event_handler.log_event(&routing_event);

    logger::debug!(
        decision_engine_euclid_selected_connector = ?crate::core::routing::format_connectors_for_logging(&euclid_response.evaluated_output),
        "decision_engine_euclid"
    );
    Ok(euclid_response)
}

/// This function transforms the decision_engine response in a way that's usable for further flows:
/// It places evaluated_output connectors first, followed by remaining output connectors (no duplicates).
pub fn transform_de_output_for_router(
    de_output: Vec<ConnectorInfo>,
    de_evaluated_output: Vec<RoutableConnectorChoice>,
) -> RoutingResult<Vec<RoutableConnectorChoice>> {
    // Keyed on the full (connector, merchant_connector_id) pair rather than the connector name
    // alone: a merchant can hold several MCAs for the same connector, and a rule that spans two
    // of them (e.g. a volume split across two paypal MCAs) must keep both.
    let mut seen = HashSet::new();

    // evaluated connectors on top, to ensure the fallback is based on other connectors.
    let mut ordered = Vec::with_capacity(de_output.len() + de_evaluated_output.len());
    for eval_conn in de_evaluated_output {
        if seen.insert((eval_conn.connector, eval_conn.merchant_connector_id.clone())) {
            ordered.push(eval_conn);
        }
    }

    // Add remaining connectors from de_output (only if not already seen), for fallback
    for conn in de_output {
        let choice = RoutableConnectorChoice::from(DeRoutableConnectorChoice::try_from(conn)?);
        if seen.insert((choice.connector, choice.merchant_connector_id.clone())) {
            ordered.push(choice);
        }
    }
    Ok(ordered)
}

/// Which call site produced a Decision Engine evaluation.
///
/// Carried into the routing event so the three flows can be told apart -- previously
/// all three logged under the same name, which made a session-flow discrepancy
/// indistinguishable from a payment one.
#[derive(Debug, Clone, Copy)]
pub enum RoutingFlow {
    Payment,
    SessionToken,
    PaymentMethodList,
}

impl RoutingFlow {
    /// Name recorded on the routing event. `Payment` keeps its historical value so
    /// existing queries over these events keep working.
    fn event_name(self) -> String {
        match self {
            Self::Payment => "DecisionEngine: Euclid Static Routing".to_string(),
            Self::SessionToken => {
                "DecisionEngine: Euclid Static Routing (session tokens)".to_string()
            }
            Self::PaymentMethodList => {
                "DecisionEngine: Euclid Static Routing (payment method list)".to_string()
            }
        }
    }
}

pub async fn decision_engine_routing(
    state: &SessionState,
    backend_input: BackendInput,
    business_profile: &domain::Profile,
    payment_id: String,
    merchant_fallback_config: Vec<RoutableConnectorChoice>,
    algorithm_for: TransactionType,
    routing_flow: RoutingFlow,
) -> RoutingResult<Vec<RoutableConnectorChoice>> {
    let routing_events_wrapper = RoutingEventsWrapper::new(
        state.tenant.tenant_id.clone(),
        state.request_id.clone(),
        payment_id.clone(),
        business_profile.get_id().to_owned(),
        business_profile.merchant_id.to_owned(),
        routing_flow.event_name(),
        None,
        true,
        false,
    );

    let de_euclid_evaluate_response = perform_decision_euclid_routing(
        state,
        backend_input.clone(),
        business_profile.get_id().get_string_repr().to_string(),
        payment_id,
        routing_events_wrapper,
        merchant_fallback_config,
        algorithm_for,
    )
    .await;

    let Ok(de_euclid_response) = de_euclid_evaluate_response else {
        logger::error!("decision_engine_euclid_evaluation_error: error in evaluation of rule");
        return Ok(Vec::default());
    };

    let de_output_connector = extract_de_output_connectors(de_euclid_response.output)
            .map_err(|e| {
                logger::error!(error=?e, "decision_engine_euclid_evaluation_error: Failed to extract connector from Output");
                e
            })?;

    transform_de_output_for_router(
            de_output_connector.clone(),
            de_euclid_response.evaluated_output.clone(),
        )
        .map_err(|e| {
            logger::error!(error=?e, "decision_engine_euclid_evaluation_error: failed to transform connector from de-output");
            e
        })
}

/// Custom deserializer for output from decision_engine, this is required as untagged enum is
/// stored but the enum requires tagged deserialization, hence deserializing it into specific
/// variants
pub fn extract_de_output_connectors(
    output_value: serde_json::Value,
) -> RoutingResult<Vec<ConnectorInfo>> {
    const SINGLE: &str = "straight_through";
    const PRIORITY: &str = "priority";
    const VOLUME_SPLIT: &str = "volume_split";
    const VOLUME_SPLIT_PRIORITY: &str = "volume_split_priority";

    let obj = output_value.as_object().ok_or_else(|| {
        logger::error!("decision_engine_euclid_error: output is not a JSON object");
        errors::RoutingError::OpenRouterError("Expected output to be a JSON object".into())
    })?;

    let type_str = obj.get("type").and_then(|v| v.as_str()).ok_or_else(|| {
        logger::error!("decision_engine_euclid_error: missing or invalid 'type' in output");
        errors::RoutingError::OpenRouterError("Missing or invalid 'type' field in output".into())
    })?;

    match type_str {
        SINGLE => {
            let connector_value = obj.get("connector").ok_or_else(|| {
                logger::error!(
                    "decision_engine_euclid_error: missing 'connector' field for type=single"
                );
                errors::RoutingError::OpenRouterError(
                    "Missing 'connector' field for single output".into(),
                )
            })?;
            let connector: ConnectorInfo = serde_json::from_value(connector_value.clone())
                .map_err(|e| {
                    logger::error!(
                        ?e,
                        "decision_engine_euclid_error: Failed to parse single connector"
                    );
                    errors::RoutingError::OpenRouterError(
                        "Failed to deserialize single connector".into(),
                    )
                })?;
            Ok(vec![connector])
        }

        PRIORITY => {
            let connectors_value = obj.get("connectors").ok_or_else(|| {
                logger::error!(
                    "decision_engine_euclid_error: missing 'connectors' field for type=priority"
                );
                errors::RoutingError::OpenRouterError(
                    "Missing 'connectors' field for priority output".into(),
                )
            })?;
            let connectors: Vec<ConnectorInfo> = serde_json::from_value(connectors_value.clone())
                .map_err(|e| {
                logger::error!(
                    ?e,
                    "decision_engine_euclid_error: Failed to parse connectors for priority"
                );
                errors::RoutingError::OpenRouterError(
                    "Failed to deserialize priority connectors".into(),
                )
            })?;
            Ok(connectors)
        }

        VOLUME_SPLIT => {
            let splits_value = obj.get("splits").ok_or_else(|| {
                logger::error!(
                    "decision_engine_euclid_error: missing 'splits' field for type=volume_split"
                );
                errors::RoutingError::OpenRouterError(
                    "Missing 'splits' field for volume_split output".into(),
                )
            })?;

            // Transform each {connector, split} into {output, split}
            let fixed_splits: Vec<_> = splits_value
                .as_array()
                .ok_or_else(|| {
                    logger::error!("decision_engine_euclid_error: 'splits' is not an array");
                    errors::RoutingError::OpenRouterError("'splits' field must be an array".into())
                })?
                .iter()
                .map(|entry| {
                    let mut entry_map = entry.as_object().cloned().ok_or_else(|| {
                        logger::error!(
                            "decision_engine_euclid_error: invalid split entry in volume_split"
                        );
                        errors::RoutingError::OpenRouterError(
                            "Invalid entry in splits array".into(),
                        )
                    })?;
                    if let Some(connector) = entry_map.remove("connector") {
                        entry_map.insert("output".to_string(), connector);
                    }
                    Ok::<_, error_stack::Report<errors::RoutingError>>(serde_json::Value::Object(
                        entry_map,
                    ))
                })
                .collect::<Result<Vec<_>, _>>()?;

            let splits: Vec<VolumeSplit<ConnectorInfo>> =
                serde_json::from_value(serde_json::Value::Array(fixed_splits)).map_err(|e| {
                    logger::error!(
                        ?e,
                        "decision_engine_euclid_error: Failed to parse volume_split"
                    );
                    errors::RoutingError::OpenRouterError(
                        "Failed to deserialize volume_split connectors".into(),
                    )
                })?;

            Ok(splits.into_iter().map(|s| s.output).collect())
        }

        VOLUME_SPLIT_PRIORITY => {
            let splits_value = obj.get("splits").ok_or_else(|| {
                logger::error!("decision_engine_euclid_error: missing 'splits' field for type=volume_split_priority");
                errors::RoutingError::OpenRouterError("Missing 'splits' field for volume_split_priority output".into())
            })?;

            // Transform each {connector: [...], split} into {output: [...], split}
            let fixed_splits: Vec<_> = splits_value
                .as_array()
                .ok_or_else(|| {
                    logger::error!("decision_engine_euclid_error: 'splits' is not an array");
                    errors::RoutingError::OpenRouterError("'splits' field must be an array".into())
                })?
                .iter()
                .map(|entry| {
                    let mut entry_map = entry.as_object().cloned().ok_or_else(|| {
                        logger::error!("decision_engine_euclid_error: invalid split entry in volume_split_priority");
                        errors::RoutingError::OpenRouterError("Invalid entry in splits array".into())
                    })?;
                    if let Some(connector) = entry_map.remove("connector") {
                        entry_map.insert("output".to_string(), connector);
                    }
                    Ok::<_, error_stack::Report<errors::RoutingError>>(serde_json::Value::Object(entry_map))
                })
                .collect::<Result<Vec<_>, _>>()?;

            let splits: Vec<VolumeSplit<Vec<ConnectorInfo>>> =
                serde_json::from_value(serde_json::Value::Array(fixed_splits)).map_err(|e| {
                    logger::error!(
                        ?e,
                        "decision_engine_euclid_error: Failed to parse volume_split_priority"
                    );
                    errors::RoutingError::OpenRouterError(
                        "Failed to deserialize volume_split_priority connectors".into(),
                    )
                })?;

            Ok(splits.into_iter().flat_map(|s| s.output).collect())
        }

        other => {
            logger::error!(type_str=%other, "decision_engine_euclid_error: unknown output type");
            Err(
                errors::RoutingError::OpenRouterError(format!("Unknown output type: {other}"))
                    .into(),
            )
        }
    }
}

pub async fn create_de_euclid_routing_algo(
    state: &SessionState,
    routing_request: &RoutingRule,
) -> RoutingResult<String> {
    logger::debug!("decision_engine_euclid: create api call for euclid routing rule creation");

    logger::debug!(decision_engine_euclid_request=?routing_request,"decision_engine_euclid");
    let events_response = EuclidApiClient::send_decision_engine_request(
        state,
        services::Method::Post,
        "routing/create",
        Some(routing_request.clone()),
        Some(EUCLID_API_TIMEOUT),
        None,
    )
    .await?;

    let euclid_response: RoutingDictionaryRecord =
        events_response
            .response
            .ok_or(errors::RoutingError::OpenRouterError(
                "Response from decision engine API is empty".to_string(),
            ))?;

    logger::debug!(decision_engine_euclid_parsed_response=?euclid_response,"decision_engine_euclid");
    Ok(euclid_response.rule_id)
}

pub async fn link_de_euclid_routing_algorithm(
    state: &SessionState,
    routing_request: ActivateRoutingConfigRequest,
) -> RoutingResult<()> {
    logger::debug!("decision_engine_euclid: link api call for euclid routing algorithm");

    EuclidApiClient::send_decision_engine_request::<_, String>(
        state,
        services::Method::Post,
        "routing/activate",
        Some(routing_request.clone()),
        Some(EUCLID_API_TIMEOUT),
        None,
    )
    .await?;

    logger::debug!(decision_engine_euclid_activated=?routing_request, "decision_engine_euclid: link_de_euclid_routing_algorithm completed");
    Ok(())
}

pub async fn deactivate_de_euclid_routing_algorithm(
    state: &SessionState,
    routing_request: DeactivateRoutingConfigRequest,
) -> RoutingResult<()> {
    logger::debug!("decision_engine_euclid: deactivate api call for euclid routing algorithm");

    EuclidApiClient::send_decision_engine_request::<_, String>(
        state,
        services::Method::Post,
        "routing/deactivate",
        Some(routing_request.clone()),
        Some(EUCLID_API_TIMEOUT),
        None,
    )
    .await?;

    logger::debug!(decision_engine_euclid_deactivated=?routing_request, "decision_engine_euclid: deactivate_de_euclid_routing_algorithm completed");
    Ok(())
}

/// Fetches DE rules for a profile as raw JSON records (no HS-representability filter).
pub async fn fetch_de_euclid_routing_records_raw(
    state: &SessionState,
    created_by: String,
    active_only: bool,
) -> RoutingResult<Vec<serde_json::Value>> {
    let path = if active_only {
        format!("routing/list/active/{created_by}")
    } else {
        format!("routing/list/{created_by}")
    };
    let events_response = EuclidApiClient::send_decision_engine_request(
        state,
        services::Method::Post,
        path.as_str(),
        None::<()>,
        Some(EUCLID_API_TIMEOUT),
        None,
    )
    .await?;

    events_response
        .response
        .ok_or(errors::RoutingError::OpenRouterError(
            "Response from decision engine API is empty".to_string(),
        ))
        .map_err(error_stack::Report::from)
}

/// Rule ids from raw DE records, including rules HS cannot represent. Existence and
/// already-active checks must use this rather than the lenient parse, or an
/// unrepresentable rule reads as absent.
pub fn de_euclid_routing_record_ids(raw_records: &[serde_json::Value]) -> Vec<String> {
    raw_records
        .iter()
        .filter_map(|record| {
            record
                .get("id")
                .and_then(|id| id.as_str())
                .map(|id| id.to_string())
        })
        .collect()
}

/// Transaction type of a raw DE record, for callers that must not drop unrepresentable rules.
pub fn de_euclid_routing_record_algorithm_for(raw: &serde_json::Value) -> Option<TransactionType> {
    raw.get("algorithm_for")
        .and_then(|value| value.as_str())
        .and_then(|value| serde_json::from_value(serde_json::Value::String(value.to_string())).ok())
}

/// Parses a raw DE record; None (with a log) for rules HS cannot represent (e.g. `ab_test`).
pub fn parse_de_euclid_routing_record(raw: serde_json::Value) -> Option<RoutingAlgorithmRecord> {
    serde_json::from_value::<RoutingAlgorithmRecord>(raw)
        .map_err(|error| {
            logger::warn!(
                ?error,
                "decision_engine_euclid: skipping DE routing record not representable in Hyperswitch"
            );
        })
        .ok()
}

/// Fetches DE rules for a profile, skipping records HS cannot represent.
pub async fn fetch_de_euclid_routing_records(
    state: &SessionState,
    created_by: String,
    active_only: bool,
) -> RoutingResult<Vec<RoutingAlgorithmRecord>> {
    Ok(
        fetch_de_euclid_routing_records_raw(state, created_by, active_only)
            .await?
            .into_iter()
            .filter_map(parse_de_euclid_routing_record)
            .collect(),
    )
}

pub async fn list_de_euclid_routing_algorithms(
    state: &SessionState,
    routing_list_request: ListRountingAlgorithmsRequest,
) -> RoutingResult<Vec<api_routing::RoutingDictionaryRecord>> {
    logger::debug!("decision_engine_euclid: list api call for euclid routing algorithms");
    let euclid_response =
        fetch_de_euclid_routing_records(state, routing_list_request.created_by, false).await?;

    Ok(euclid_response
        .into_iter()
        .map(routing_algorithm::RoutingProfileMetadata::from)
        .map(ForeignInto::foreign_into)
        .collect::<Vec<_>>())
}

pub async fn list_de_euclid_active_routing_algorithm(
    state: &SessionState,
    created_by: String,
) -> RoutingResult<Vec<api_routing::RoutingDictionaryRecord>> {
    logger::debug!("decision_engine_euclid: list api call for euclid active routing algorithm");
    let response = fetch_de_euclid_routing_records(state, created_by, true).await?;

    Ok(response
        .into_iter()
        .map(|record| routing_algorithm::RoutingProfileMetadata::from(record).foreign_into())
        .collect())
}

/// One evaluation in a batch request: the parameters that differ per call.
/// Everything shared (`created_by`, fallback, transaction type) lives on the
/// enclosing request, matching the Decision Engine's `/routing/evaluate/batch`.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RoutingEvaluateBatchEntry {
    pub payment_id: Option<String>,
    pub parameters: HashMap<String, Option<ValueType>>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RoutingEvaluateBatchRequest {
    pub created_by: String,
    pub fallback_output: Vec<DeRoutableConnectorChoice>,
    pub algorithm_for: TransactionType,
    pub requests: Vec<RoutingEvaluateBatchEntry>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RoutingEvaluateBatchResponse {
    pub results: Vec<RoutingEvaluateResponse>,
}

/// Evaluates one rule against several parameter sets in a single Decision Engine
/// round trip. The engine fetches and parses the active algorithm once, so a session
/// request with N wallet types costs one call and one rule lookup instead of N each.
///
/// Results come back positionally: `results[i]` answers `backend_inputs[i]`. An entry
/// the engine could not evaluate is an empty list, mirroring how callers of the
/// single-call path treat a failed evaluation.
pub async fn decision_engine_routing_batch(
    state: &SessionState,
    backend_inputs: Vec<BackendInput>,
    business_profile: &domain::Profile,
    payment_id: String,
    merchant_fallback_config: Vec<RoutableConnectorChoice>,
    algorithm_for: TransactionType,
    routing_flow: RoutingFlow,
) -> RoutingResult<Vec<Vec<RoutableConnectorChoice>>> {
    // Callers build one entry per payment method type, so a card-only profile has none.
    if backend_inputs.is_empty() {
        return Ok(Vec::new());
    }

    let expected_len = backend_inputs.len();
    let created_by = business_profile.get_id().get_string_repr().to_string();
    let fallback_output = convert_fallback_to_de_choices(merchant_fallback_config);

    let requests = backend_inputs
        .into_iter()
        .map(|backend_input| {
            convert_backend_input_to_routing_eval(
                created_by.clone(),
                Some(payment_id.clone()),
                backend_input,
                fallback_output.clone(),
                algorithm_for,
            )
            .map(|request| RoutingEvaluateBatchEntry {
                payment_id: request.payment_id,
                parameters: request.parameters,
            })
        })
        .collect::<RoutingResult<Vec<_>>>()?;

    let batch_request = RoutingEvaluateBatchRequest {
        created_by,
        fallback_output,
        algorithm_for,
        requests,
    };

    let mut events_wrapper: RoutingEventsWrapper<RoutingEvaluateBatchRequest> =
        RoutingEventsWrapper::new(
            state.tenant.tenant_id.clone(),
            state.request_id.clone(),
            payment_id,
            business_profile.get_id().to_owned(),
            business_profile.merchant_id.to_owned(),
            routing_flow.event_name(),
            None,
            true,
            false,
        );
    events_wrapper.set_request_body(batch_request.clone());

    let event_response = EuclidApiClient::send_decision_engine_request::<
        RoutingEvaluateBatchRequest,
        RoutingEvaluateBatchResponse,
    >(
        state,
        services::Method::Post,
        "routing/evaluate/batch",
        Some(batch_request),
        Some(EUCLID_API_TIMEOUT),
        Some(events_wrapper),
    )
    .await?;

    let batch_response = event_response
        .response
        .ok_or(errors::RoutingError::OpenRouterError(
            "Response from decision engine batch API is empty".to_string(),
        ))?;

    if batch_response.results.len() != expected_len {
        return Err(errors::RoutingError::OpenRouterError(format!(
            "decision engine batch returned {} results for {} requests",
            batch_response.results.len(),
            expected_len
        ))
        .into());
    }

    let transformed = batch_response
        .results
        .into_iter()
        .map(|entry| {
            // A per-entry failure is an empty list rather than a batch failure: one
            // wallet type falling back must not take the others down with it.
            if entry.status == "error" {
                return Vec::new();
            }
            extract_de_output_connectors(entry.output)
                .and_then(|output| transform_de_output_for_router(output, entry.evaluated_output))
                .map_err(|error| {
                    logger::error!(
                        ?error,
                        "decision_engine_euclid: failed to transform a batch entry"
                    );
                })
                .unwrap_or_default()
        })
        .collect::<Vec<_>>();

    if let Some(mut routing_event) = event_response.event {
        routing_event.set_routing_approach(RoutingApproach::StaticRouting.to_string());
        routing_event
            .set_routable_connectors(transformed.iter().flatten().cloned().collect::<Vec<_>>());
        state.event_handler.log_event(&routing_event);
    }

    Ok(transformed)
}

/// The batch call, degrading to concurrent single evaluations when the engine does
/// not expose the batch endpoint yet. Lets Hyperswitch deploy ahead of the engine:
/// against an old engine every batch call fails once and the singles carry the
/// request, and once the engine ships the endpoint the fallback stops firing.
pub async fn decision_engine_routing_batch_with_fallback(
    state: &SessionState,
    backend_inputs: Vec<BackendInput>,
    business_profile: &domain::Profile,
    payment_id: String,
    merchant_fallback_config: Vec<RoutableConnectorChoice>,
    algorithm_for: TransactionType,
    routing_flow: RoutingFlow,
) -> Vec<Vec<RoutableConnectorChoice>> {
    match decision_engine_routing_batch(
        state,
        backend_inputs.clone(),
        business_profile,
        payment_id.clone(),
        merchant_fallback_config.clone(),
        algorithm_for,
        routing_flow,
    )
    .await
    {
        Ok(results) => results,
        Err(error) => {
            logger::warn!(
                ?error,
                "decision_engine_euclid: batch evaluate failed, falling back to single evaluations"
            );
            futures::future::join_all(backend_inputs.into_iter().map(|backend_input| {
                decision_engine_routing(
                    state,
                    backend_input,
                    business_profile,
                    payment_id.clone(),
                    merchant_fallback_config.clone(),
                    algorithm_for,
                    routing_flow,
                )
            }))
            .await
            .into_iter()
            .map(|result| {
                result
                    .inspect_err(|error| {
                        logger::error!(
                            ?error,
                            "decision_engine_euclid: single evaluation failed in batch fallback"
                        );
                    })
                    .unwrap_or_default()
            })
            .collect()
        }
    }
}

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ActivateRoutingConfigRequest {
    pub created_by: String,
    pub routing_algorithm_id: String,
}

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct DeactivateRoutingConfigRequest {
    pub created_by: String,
    pub routing_algorithm_id: String,
}

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ListRountingAlgorithmsRequest {
    pub created_by: String,
}

// Maps Hyperswitch `BackendInput` to a `RoutingEvaluateRequest` compatible with Decision Engine
pub fn convert_backend_input_to_routing_eval(
    created_by: String,
    payment_id: Option<String>,
    input: BackendInput,
    fallback_output: Vec<DeRoutableConnectorChoice>,
    algorithm_for: TransactionType,
) -> RoutingResult<RoutingEvaluateRequest> {
    let mut params: HashMap<String, Option<ValueType>> = HashMap::new();

    // Payment
    params.insert(
        "amount".to_string(),
        Some(ValueType::Number(
            input
                .payment
                .amount
                .get_amount_as_i64()
                .try_into()
                .unwrap_or_default(),
        )),
    );
    params.insert(
        "currency".to_string(),
        Some(ValueType::EnumVariant(input.payment.currency.to_string())),
    );

    if let Some(auth_type) = input.payment.authentication_type {
        params.insert(
            "authentication_type".to_string(),
            Some(ValueType::EnumVariant(auth_type.to_string())),
        );
    }
    if let Some(extended_bin) = input.payment.extended_card_bin {
        params.insert(
            "extended_card_bin".to_string(),
            Some(ValueType::StrValue(extended_bin)),
        );
    }
    if let Some(bin) = input.payment.card_bin {
        params.insert("card_bin".to_string(), Some(ValueType::StrValue(bin)));
    }
    if let Some(capture_method) = input.payment.capture_method {
        params.insert(
            "capture_method".to_string(),
            Some(ValueType::EnumVariant(capture_method.to_string())),
        );
    }
    if let Some(country) = input.payment.business_country {
        params.insert(
            "business_country".to_string(),
            Some(ValueType::EnumVariant(country.to_string())),
        );
    }
    if let Some(country) = input.payment.billing_country {
        params.insert(
            "billing_country".to_string(),
            Some(ValueType::EnumVariant(country.to_string())),
        );
    }
    if let Some(label) = input.payment.business_label {
        params.insert(
            "business_label".to_string(),
            Some(ValueType::StrValue(label)),
        );
    }
    if let Some(sfu) = input.payment.setup_future_usage {
        params.insert(
            "setup_future_usage".to_string(),
            Some(ValueType::EnumVariant(sfu.to_string())),
        );
    }
    if let Some(surcharge_amount) = input.payment.surcharge_amount {
        params.insert(
            "surcharge_amount".to_string(),
            Some(ValueType::Number(
                surcharge_amount
                    .get_amount_as_i64()
                    .try_into()
                    .unwrap_or_default(),
            )),
        );
    }
    if let Some(transaction_initiator) = input.payment.transaction_initiator {
        params.insert(
            "transaction_initiator".to_string(),
            Some(ValueType::EnumVariant(transaction_initiator.to_string())),
        );
    }

    // PaymentMethod
    if let Some(pm) = input.payment_method.payment_method {
        params.insert(
            "payment_method".to_string(),
            Some(ValueType::EnumVariant(pm.to_string())),
        );
        if let Some(pmt) = input.payment_method.payment_method_type {
            match (pmt, pm).into_dir_value() {
                Ok(dv) => insert_dirvalue_param(&mut params, dv),
                Err(e) => logger::debug!(
                    ?e,
                    ?pmt,
                    ?pm,
                    "decision_engine_euclid: into_dir_value failed; skipping subset param"
                ),
            }
        }
    }
    if let Some(pmt) = input.payment_method.payment_method_type {
        params.insert(
            "payment_method_type".to_string(),
            Some(ValueType::EnumVariant(pmt.to_string())),
        );
    }
    if let Some(network) = input.payment_method.card_network {
        params.insert(
            "card_network".to_string(),
            Some(ValueType::EnumVariant(network.to_string())),
        );
    }
    if let Some(card_discovery) = input.payment_method.card_discovery {
        params.insert(
            "card_discovery".to_string(),
            Some(ValueType::EnumVariant(card_discovery.to_string())),
        );
    }

    // Mandate
    if let Some(pt) = input.mandate.payment_type {
        params.insert(
            "payment_type".to_string(),
            Some(ValueType::EnumVariant(pt.to_string())),
        );
    }
    if let Some(mt) = input.mandate.mandate_type {
        params.insert(
            "mandate_type".to_string(),
            Some(ValueType::EnumVariant(mt.to_string())),
        );
    }
    if let Some(mat) = input.mandate.mandate_acceptance_type {
        params.insert(
            "mandate_acceptance_type".to_string(),
            Some(ValueType::EnumVariant(mat.to_string())),
        );
    }

    // Issuer
    if let Some(country) = input
        .issuer_data
        .as_ref()
        .and_then(|data| data.country.as_ref())
    {
        params.insert(
            "issuer_country".to_string(),
            Some(ValueType::EnumVariant(country.to_string())),
        );
    }

    // Metadata
    if let Some(meta) = input.metadata {
        for (k, v) in meta.into_iter() {
            params.insert(
                k.clone(),
                Some(ValueType::MetadataVariant(MetadataValue {
                    key: k,
                    value: v,
                })),
            );
        }
    }

    Ok(RoutingEvaluateRequest {
        created_by,
        payment_id,
        parameters: params,
        fallback_output,
        algorithm_for: Some(algorithm_for),
    })
}

// All the independent variants of payment method types, configured via dashboard
fn insert_dirvalue_param(params: &mut HashMap<String, Option<ValueType>>, dv: dir::DirValue) {
    match dv {
        dir::DirValue::RewardType(v) => {
            params.insert(
                "reward".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::CardType(v) => {
            params.insert(
                "card_type".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::PayLaterType(v) => {
            params.insert(
                "pay_later".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::WalletType(v) => {
            params.insert(
                "wallet".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::VoucherType(v) => {
            params.insert(
                "voucher".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::BankRedirectType(v) => {
            params.insert(
                "bank_redirect".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::BankDebitType(v) => {
            params.insert(
                "bank_debit".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::BankTransferType(v) => {
            params.insert(
                "bank_transfer".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::RealTimePaymentType(v) => {
            params.insert(
                "real_time_payment".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::UpiType(v) => {
            params.insert(
                "upi".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::GiftCardType(v) => {
            params.insert(
                "gift_card".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::CardRedirectType(v) => {
            params.insert(
                "card_redirect".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::OpenBankingType(v) => {
            params.insert(
                "open_banking".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::MobilePaymentType(v) => {
            params.insert(
                "mobile_payment".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::CryptoType(v) => {
            params.insert(
                "crypto".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        dir::DirValue::NetworkTokenType(v) => {
            params.insert(
                "network_token".to_string(),
                Some(ValueType::EnumVariant(v.to_string())),
            );
        }
        other => {
            // all other values can be ignored for now as they don't converge with
            // payment method type
            logger::warn!(
                ?other,
                "decision_engine_euclid: unmapped dir::DirValue; add a mapping here"
            );
        }
    }
}

#[derive(Debug, Clone, serde::Deserialize)]
struct DeErrorResponse {
    code: String,
    message: String,
    data: Option<serde_json::Value>,
}

impl DecisionEngineErrorsInterface for DeErrorResponse {
    fn get_error_message(&self) -> String {
        self.message.clone()
    }

    fn get_error_code(&self) -> String {
        self.code.clone()
    }

    fn get_error_data(&self) -> Option<String> {
        self.data.as_ref().map(|data| data.to_string())
    }
}

impl DecisionEngineErrorsInterface for or_types::ErrorResponse {
    fn get_error_message(&self) -> String {
        self.error_message.clone()
    }

    fn get_error_code(&self) -> String {
        self.error_code.clone()
    }

    fn get_error_data(&self) -> Option<String> {
        Some(format!(
            "decision_engine Error: {}",
            self.error_message.clone()
        ))
    }
}

pub type Metadata = HashMap<String, serde_json::Value>;

/// Represents a single comparison condition.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct Comparison {
    /// The left hand side which will always be a domain input identifier like "payment.method.cardtype"
    pub lhs: String,
    /// The comparison operator
    pub comparison: ComparisonType,
    /// The value to compare against
    pub value: ValueType,
    /// Additional metadata that the Static Analyzer and Backend does not touch.
    /// This can be used to store useful information for the frontend and is required for communication
    /// between the static analyzer and the frontend.
    // #[schema(value_type=HashMap<String, serde_json::Value>)]
    pub metadata: Metadata,
}

/// Represents all the conditions of an IF statement
/// eg:
///
/// ```text
/// payment.method = card & payment.method.cardtype = debit & payment.method.network = diners
/// ```
pub type IfCondition = Vec<Comparison>;

/// Represents an IF statement with conditions and optional nested IF statements
///
/// ```text
/// payment.method = card {
///     payment.method.cardtype = (credit, debit) {
///         payment.method.network = (amex, rupay, diners)
///     }
/// }
/// ```
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct IfStatement {
    // #[schema(value_type=Vec<Comparison>)]
    pub condition: IfCondition,
    pub nested: Option<Vec<Self>>,
}

/// Represents a rule
///
/// ```text
/// rule_name: [stripe, adyen, checkout]
/// {
///     payment.method = card {
///         payment.method.cardtype = (credit, debit) {
///             payment.method.network = (amex, rupay, diners)
///         }
///
///         payment.method.cardtype = credit
///     }
/// }
/// ```
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
// #[aliases(RuleConnectorSelection = Rule<ConnectorSelection>)]
pub struct Rule {
    pub name: String,
    #[serde(alias = "routingType")]
    pub routing_type: RoutingType,
    #[serde(alias = "routingOutput")]
    pub output: Output,
    pub statements: Vec<IfStatement>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RoutingType {
    Priority,
    VolumeSplit,
    VolumeSplitPriority,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct VolumeSplit<T> {
    pub split: u8,
    pub output: T,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct ConnectorInfo {
    pub gateway_name: String,
    pub gateway_id: Option<String>,
}

impl TryFrom<ConnectorInfo> for DeRoutableConnectorChoice {
    type Error = error_stack::Report<errors::RoutingError>;

    fn try_from(c: ConnectorInfo) -> Result<Self, Self::Error> {
        let gateway_id = c
            .gateway_id
            .map(|mca| {
                id_type::MerchantConnectorAccountId::wrap(mca)
                    .change_context(errors::RoutingError::GenericConversionError {
                        from: "String".to_string(),
                        to: "MerchantConnectorAccountId".to_string(),
                    })
                    .attach_printable("unable to convert MerchantConnectorAccountId from string")
            })
            .transpose()?;

        let gateway_name = RoutableConnectors::from_str(&c.gateway_name)
            .map_err(|error| {
                logger::error!(
                    error=?error,
                    connector = %crate::core::routing::format_connector_for_logging(&c.gateway_name, gateway_id.as_ref()),
                    "euclid: unable to convert connector name to RoutableConnectors"
                );
                errors::RoutingError::GenericConversionError {
                    from: "String".to_string(),
                    to: "RoutableConnectors".to_string(),
                }
            })
            .attach_printable("unable to convert connector name to RoutableConnectors")?;

        Ok(Self {
            gateway_name,
            gateway_id,
        })
    }
}

impl ConnectorInfo {
    pub fn new(gateway_name: String, gateway_id: Option<String>) -> Self {
        Self {
            gateway_name,
            gateway_id,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Output {
    Single(ConnectorInfo),
    Priority(Vec<ConnectorInfo>),
    VolumeSplit(Vec<VolumeSplit<ConnectorInfo>>),
    VolumeSplitPriority(Vec<VolumeSplit<Vec<ConnectorInfo>>>),
}

pub type Globals = HashMap<String, HashSet<ValueType>>;

/// The program, having a default connector selection and
/// a bunch of rules. Also can hold arbitrary metadata.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
// #[aliases(ProgramConnectorSelection = Program<ConnectorSelection>)]
pub struct Program {
    pub globals: Globals,
    pub default_selection: Output,
    // #[schema(value_type=RuleConnectorSelection)]
    pub rules: Vec<Rule>,
    // #[schema(value_type=HashMap<String, serde_json::Value>)]
    pub metadata: Option<Metadata>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct RoutingRule {
    pub rule_id: Option<String>,
    pub name: String,
    pub description: Option<String>,
    pub metadata: Option<RoutingMetadata>,
    pub created_by: String,
    #[serde(default)]
    pub algorithm_for: AlgorithmType,
    pub algorithm: StaticRoutingAlgorithm,
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, strum::Display)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum AlgorithmType {
    #[default]
    Payment,
    Payout,
    ThreeDsAuthentication,
}

impl From<TransactionType> for AlgorithmType {
    fn from(transaction_type: TransactionType) -> Self {
        match transaction_type {
            TransactionType::Payment => Self::Payment,
            TransactionType::Payout => Self::Payout,
            TransactionType::ThreeDsAuthentication => Self::ThreeDsAuthentication,
        }
    }
}

impl From<RoutableConnectorChoice> for ConnectorInfo {
    fn from(c: RoutableConnectorChoice) -> Self {
        Self {
            gateway_name: c.connector.to_string(),
            gateway_id: c
                .merchant_connector_id
                .map(|mca_id| mca_id.get_string_repr().to_string()),
        }
    }
}

impl From<Box<RoutableConnectorChoice>> for ConnectorInfo {
    fn from(c: Box<RoutableConnectorChoice>) -> Self {
        Self {
            gateway_name: c.connector.to_string(),
            gateway_id: c
                .merchant_connector_id
                .map(|mca_id| mca_id.get_string_repr().to_string()),
        }
    }
}

impl From<ConnectorVolumeSplit> for VolumeSplit<ConnectorInfo> {
    fn from(v: ConnectorVolumeSplit) -> Self {
        Self {
            split: v.split,
            output: ConnectorInfo {
                gateway_name: v.connector.connector.to_string(),
                gateway_id: v
                    .connector
                    .merchant_connector_id
                    .map(|mca_id| mca_id.get_string_repr().to_string()),
            },
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data", rename_all = "snake_case")]
pub enum StaticRoutingAlgorithm {
    Single(Box<ConnectorInfo>),
    Priority(Vec<ConnectorInfo>),
    VolumeSplit(Vec<VolumeSplit<ConnectorInfo>>),
    Advanced(Program),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct RoutingMetadata {
    pub kind: enums::RoutingAlgorithmKind,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct RoutingDictionaryRecord {
    pub rule_id: String,
    pub name: String,
    pub created_at: time::PrimitiveDateTime,
    pub modified_at: time::PrimitiveDateTime,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct RoutingAlgorithmRecord {
    pub id: id_type::RoutingId,
    pub name: String,
    pub description: Option<String>,
    pub created_by: id_type::ProfileId,
    pub algorithm_data: StaticRoutingAlgorithm,
    pub algorithm_for: TransactionType,
    pub metadata: Option<RoutingMetadata>,
    pub created_at: time::PrimitiveDateTime,
    pub modified_at: time::PrimitiveDateTime,
}

impl From<RoutingAlgorithmRecord> for routing_algorithm::RoutingProfileMetadata {
    fn from(record: RoutingAlgorithmRecord) -> Self {
        let kind = match record.algorithm_data {
            StaticRoutingAlgorithm::Single(_) => enums::RoutingAlgorithmKind::Single,
            StaticRoutingAlgorithm::Priority(_) => enums::RoutingAlgorithmKind::Priority,
            StaticRoutingAlgorithm::VolumeSplit(_) => enums::RoutingAlgorithmKind::VolumeSplit,
            StaticRoutingAlgorithm::Advanced(_) => enums::RoutingAlgorithmKind::Advanced,
        };
        Self {
            profile_id: record.created_by,
            algorithm_id: record.id,
            name: record.name,
            description: record.description,
            kind,
            created_at: record.created_at,
            modified_at: record.modified_at,
            algorithm_for: record.algorithm_for,
        }
    }
}

impl TryFrom<ast::Program<ConnectorSelection>> for Program {
    type Error = error_stack::Report<errors::RoutingError>;

    fn try_from(p: ast::Program<ConnectorSelection>) -> Result<Self, Self::Error> {
        let rules = p
            .rules
            .into_iter()
            .map(convert_rule)
            .collect::<Result<Vec<_>, _>>()?;

        Ok(Self {
            globals: HashMap::new(),
            default_selection: convert_output(p.default_selection),
            rules,
            metadata: Some(p.metadata),
        })
    }
}

impl TryFrom<ast::Program<ConnectorSelection>> for StaticRoutingAlgorithm {
    type Error = error_stack::Report<errors::RoutingError>;

    fn try_from(p: ast::Program<ConnectorSelection>) -> Result<Self, Self::Error> {
        let internal_program: Program = p.try_into()?;
        Ok(Self::Advanced(internal_program))
    }
}

fn convert_rule(rule: ast::Rule<ConnectorSelection>) -> RoutingResult<Rule> {
    let routing_type = match &rule.connector_selection {
        ConnectorSelection::Priority(_) => RoutingType::Priority,
        ConnectorSelection::VolumeSplit(_) => RoutingType::VolumeSplit,
    };

    Ok(Rule {
        name: rule.name,
        routing_type,
        output: convert_output(rule.connector_selection),
        statements: rule
            .statements
            .into_iter()
            .map(convert_if_stmt)
            .collect::<RoutingResult<Vec<IfStatement>>>()?,
    })
}

fn convert_if_stmt(stmt: ast::IfStatement) -> RoutingResult<IfStatement> {
    Ok(IfStatement {
        condition: stmt
            .condition
            .into_iter()
            .map(convert_comparison)
            .collect::<RoutingResult<Vec<Comparison>>>()?,

        nested: stmt
            .nested
            .map(|v| {
                v.into_iter()
                    .map(convert_if_stmt)
                    .collect::<RoutingResult<Vec<IfStatement>>>()
            })
            .transpose()?,
    })
}

fn convert_comparison(c: ast::Comparison) -> RoutingResult<Comparison> {
    Ok(Comparison {
        lhs: c.lhs,
        comparison: convert_comparison_type(c.comparison),
        value: convert_value(c.value)?,
        metadata: c.metadata,
    })
}

fn convert_comparison_type(ct: ast::ComparisonType) -> ComparisonType {
    match ct {
        ast::ComparisonType::Equal => ComparisonType::Equal,
        ast::ComparisonType::NotEqual => ComparisonType::NotEqual,
        ast::ComparisonType::LessThan => ComparisonType::LessThan,
        ast::ComparisonType::LessThanEqual => ComparisonType::LessThanEqual,
        ast::ComparisonType::GreaterThan => ComparisonType::GreaterThan,
        ast::ComparisonType::GreaterThanEqual => ComparisonType::GreaterThanEqual,
    }
}

fn convert_value(v: ast::ValueType) -> RoutingResult<ValueType> {
    use ast::ValueType::*;
    match v {
        Number(n) => Ok(ValueType::Number(
            n.get_amount_as_i64().try_into().unwrap_or_default(),
        )),
        EnumVariant(e) => Ok(ValueType::EnumVariant(e)),
        MetadataVariant(m) => Ok(ValueType::MetadataVariant(MetadataValue {
            key: m.key,
            value: m.value,
        })),
        StrValue(s) => Ok(ValueType::StrValue(s)),

        NumberArray(arr) => Ok(ValueType::NumberArray(
            arr.into_iter()
                .map(|n| n.get_amount_as_i64().try_into().unwrap_or_default())
                .collect(),
        )),
        EnumVariantArray(arr) => Ok(ValueType::EnumVariantArray(arr)),
        NumberComparisonArray(arr) => Ok(ValueType::NumberComparisonArray(
            arr.into_iter()
                .map(|nc| NumberComparison {
                    comparison_type: convert_comparison_type(nc.comparison_type),
                    number: nc.number.get_amount_as_i64().try_into().unwrap_or_default(),
                })
                .collect(),
        )),
    }
}

fn convert_output(sel: ConnectorSelection) -> Output {
    match sel {
        ConnectorSelection::Priority(choices) => {
            Output::Priority(choices.into_iter().map(stringify_choice).collect())
        }
        ConnectorSelection::VolumeSplit(vs) => Output::VolumeSplit(
            vs.into_iter()
                .map(|v| VolumeSplit {
                    split: v.split,
                    output: stringify_choice(v.connector),
                })
                .collect(),
        ),
    }
}

fn stringify_choice(c: RoutableConnectorChoice) -> ConnectorInfo {
    ConnectorInfo::new(
        c.connector.to_string(),
        c.merchant_connector_id
            .map(|mca_id| mca_id.get_string_repr().to_string()),
    )
}

// Reverse of the transformers above: rebuilds a euclid AST from a Decision Engine
// program, so a rule authored on the DE can be served through the same typed
// response shape as a Hyperswitch-authored one.
impl TryFrom<Program> for ast::Program<ConnectorSelection> {
    type Error = error_stack::Report<errors::RoutingError>;

    fn try_from(p: Program) -> Result<Self, Self::Error> {
        if !p.globals.is_empty() {
            // Globals are only reachable through `ValueType::GlobalRef`, which
            // `convert_value_back` rejects, so an unreferenced map is safe to drop.
            logger::debug!("euclid: dropping unreferenced globals while converting DE program");
        }

        Ok(Self {
            default_selection: convert_output_back(p.default_selection)?,
            rules: p
                .rules
                .into_iter()
                .map(convert_rule_back)
                .collect::<RoutingResult<Vec<_>>>()?,
            metadata: p.metadata.unwrap_or_default(),
        })
    }
}

fn convert_rule_back(rule: Rule) -> RoutingResult<ast::Rule<ConnectorSelection>> {
    Ok(ast::Rule {
        name: rule.name,
        // `routing_type` carries no information the output does not already hold.
        connector_selection: convert_output_back(rule.output)?,
        statements: rule
            .statements
            .into_iter()
            .map(convert_if_stmt_back)
            .collect::<RoutingResult<Vec<_>>>()?,
    })
}

fn convert_if_stmt_back(stmt: IfStatement) -> RoutingResult<ast::IfStatement> {
    Ok(ast::IfStatement {
        condition: stmt
            .condition
            .into_iter()
            .map(convert_comparison_back)
            .collect::<RoutingResult<Vec<_>>>()?,
        nested: stmt
            .nested
            .map(|v| {
                v.into_iter()
                    .map(convert_if_stmt_back)
                    .collect::<RoutingResult<Vec<_>>>()
            })
            .transpose()?,
    })
}

fn convert_comparison_back(c: Comparison) -> RoutingResult<ast::Comparison> {
    Ok(ast::Comparison {
        lhs: c.lhs,
        comparison: convert_comparison_type_back(c.comparison),
        value: convert_value_back(c.value)?,
        metadata: c.metadata,
    })
}

fn convert_comparison_type_back(ct: ComparisonType) -> ast::ComparisonType {
    match ct {
        ComparisonType::Equal => ast::ComparisonType::Equal,
        ComparisonType::NotEqual => ast::ComparisonType::NotEqual,
        ComparisonType::LessThan => ast::ComparisonType::LessThan,
        ComparisonType::LessThanEqual => ast::ComparisonType::LessThanEqual,
        ComparisonType::GreaterThan => ast::ComparisonType::GreaterThan,
        ComparisonType::GreaterThanEqual => ast::ComparisonType::GreaterThanEqual,
    }
}

/// DE amounts are `u64`; euclid's `MinorUnit` is `i64`. Anything above `i64::MAX`
/// would wrap to a negative amount, so it is rejected rather than silently mangled.
fn de_number_to_minor_unit(n: u64) -> RoutingResult<MinorUnit> {
    i64::try_from(n)
        .map(MinorUnit::new)
        .map_err(|error| {
            logger::error!(
                error = ?error,
                value = %n,
                "euclid: DE number does not fit in MinorUnit"
            );
            errors::RoutingError::GenericConversionError {
                from: "u64".to_string(),
                to: "MinorUnit".to_string(),
            }
        })
        .map_err(error_stack::Report::from)
        .attach_printable("DE number out of range for MinorUnit")
}

fn convert_value_back(v: ValueType) -> RoutingResult<ast::ValueType> {
    match v {
        ValueType::Number(n) => Ok(ast::ValueType::Number(de_number_to_minor_unit(n)?)),
        ValueType::EnumVariant(e) => Ok(ast::ValueType::EnumVariant(e)),
        ValueType::MetadataVariant(m) => Ok(ast::ValueType::MetadataVariant(ast::MetadataValue {
            key: m.key,
            value: m.value,
        })),
        ValueType::StrValue(s) => Ok(ast::ValueType::StrValue(s)),
        ValueType::NumberArray(arr) => Ok(ast::ValueType::NumberArray(
            arr.into_iter()
                .map(de_number_to_minor_unit)
                .collect::<RoutingResult<Vec<_>>>()?,
        )),
        ValueType::EnumVariantArray(arr) => Ok(ast::ValueType::EnumVariantArray(arr)),
        ValueType::NumberComparisonArray(arr) => Ok(ast::ValueType::NumberComparisonArray(
            arr.into_iter()
                .map(|nc| {
                    Ok(ast::NumberComparison {
                        comparison_type: convert_comparison_type_back(nc.comparison_type),
                        number: de_number_to_minor_unit(nc.number)?,
                    })
                })
                .collect::<RoutingResult<Vec<_>>>()?,
        )),
        // Euclid's AST has no global-reference form.
        ValueType::GlobalRef(name) => Err(error_stack::Report::from(
            errors::RoutingError::GenericConversionError {
                from: "ValueType::GlobalRef".to_string(),
                to: "euclid ValueType".to_string(),
            },
        ))
        .attach_printable_lazy(|| format!("unsupported global reference {name} in DE program")),
    }
}

fn convert_output_back(output: Output) -> RoutingResult<ConnectorSelection> {
    match output {
        // Euclid has no single-connector output; a one-element priority list is equivalent.
        Output::Single(info) => Ok(ConnectorSelection::Priority(vec![parse_choice(info)?])),
        Output::Priority(infos) => Ok(ConnectorSelection::Priority(
            infos
                .into_iter()
                .map(parse_choice)
                .collect::<RoutingResult<Vec<_>>>()?,
        )),
        Output::VolumeSplit(splits) => Ok(ConnectorSelection::VolumeSplit(
            splits
                .into_iter()
                .map(|vs| {
                    Ok(ConnectorVolumeSplit {
                        connector: parse_choice(vs.output)?,
                        split: vs.split,
                    })
                })
                .collect::<RoutingResult<Vec<_>>>()?,
        )),
        Output::VolumeSplitPriority(_) => Err(error_stack::Report::from(
            errors::RoutingError::GenericConversionError {
                from: "Output::VolumeSplitPriority".to_string(),
                to: "ConnectorSelection".to_string(),
            },
        ))
        .attach_printable("euclid has no volume-split-priority connector selection"),
    }
}

fn parse_choice(info: ConnectorInfo) -> RoutingResult<RoutableConnectorChoice> {
    DeRoutableConnectorChoice::try_from(info).map(RoutableConnectorChoice::from)
}

pub async fn get_routing_result_source(
    state: &SessionState,
    dimensions: &dimension_state::DimensionsWithProcessorAndProviderMerchantIdAndProfileId,
) -> api_routing::RoutingResultSource {
    // No customer_id and payment_id in call sites so passing Targeting key as None.
    dimensions
        .get_routing_result_source(
            state.store.as_ref(),
            state.superposition_service.as_ref(),
            None,
        )
        .await
}

/// Checks whether preferred-connector routing is enabled for this profile.
pub async fn is_preferred_connectors_routing_enabled(
    state: &SessionState,
    dimensions: &dimension_state::DimensionsWithProcessorAndProviderMerchantIdAndProfileId,
) -> bool {
    dimensions
        .get_preferred_connectors_routing_enabled(
            state.store.as_ref(),
            state.superposition_service.as_ref(),
            None,
        )
        .await
}

/// Effective cutover routing_result_source is DecisionEngine and either global routing flag (static or dynamic) is on - the flags always win, for APIs and payment paths alike.
pub async fn is_decision_engine_routing_effective(
    state: &SessionState,
    dimensions: &dimension_state::DimensionsWithProcessorAndProviderMerchantIdAndProfileId,
) -> bool {
    (state.conf.open_router.static_routing_enabled
        || state.conf.open_router.dynamic_routing_enabled)
        && matches!(
            get_routing_result_source(state, dimensions).await,
            api_routing::RoutingResultSource::DecisionEngine
        )
}
pub async fn select_routing_result<T>(
    state: &SessionState,
    dimensions: &dimension_state::DimensionsWithProcessorAndProviderMerchantIdAndProfileId,
    business_profile: &business_profile::Profile,
    hyperswitch_result: T,
    de_result: T,
) -> T
where
    T: Clone + IntoIterator,
{
    // Same predicate as every other consumer: with both global flags off the profile is
    // Hyperswitch-routed, so reads must not serve DE records the payment path ignores.
    let routing_result_source = if is_decision_engine_routing_effective(state, dimensions).await {
        api_routing::RoutingResultSource::DecisionEngine
    } else {
        api_routing::RoutingResultSource::HyperswitchRouting
    };

    match routing_result_source {
        api_routing::RoutingResultSource::DecisionEngine => {
            logger::debug!(
                business_profile_id=?business_profile.get_id(),
                "decision_engine_euclid: Using Decision Engine routing result"
            );

            let is_de_result_empty = de_result.clone().into_iter().next().is_none();
            if is_de_result_empty {
                // Warn, not debug: this is a cut-over profile being routed by Hyperswitch
                // because the engine returned nothing -- unreachable, or the profile's rules
                // are not migrated yet. Under DE-only writes the Hyperswitch rule is frozen
                // at the moment of cutover, so this needs to be visible rather than silent.
                logger::warn!(
                    business_profile_id=?business_profile.get_id(),
                    "decision_engine_euclid: DE result empty for a cut-over profile, serving the Hyperswitch result"
                );
                hyperswitch_result
            } else {
                de_result
            }
        }
        api_routing::RoutingResultSource::HyperswitchRouting => {
            logger::debug!(
                business_profile_id=?business_profile.get_id(),
                "decision_engine_euclid: Using Hyperswitch routing result"
            );
            hyperswitch_result
        }
    }
}

pub trait DecisionEngineErrorsInterface {
    fn get_error_message(&self) -> String;
    fn get_error_code(&self) -> String;
    fn get_error_data(&self) -> Option<String>;
}

#[derive(Debug)]
pub struct RoutingEventsWrapper<Req>
where
    Req: Serialize + Clone,
{
    pub tenant_id: id_type::TenantId,
    pub request_id: Option<RequestId>,
    pub payment_id: String,
    pub profile_id: id_type::ProfileId,
    pub merchant_id: id_type::MerchantId,
    pub flow: String,
    pub request: Option<Req>,
    pub parse_response: bool,
    pub log_event: bool,
    pub routing_event: Option<routing_events::RoutingEvent>,
}

#[derive(Debug)]
pub enum EventResponseType<Res>
where
    Res: Serialize + serde::de::DeserializeOwned + Clone,
{
    Structured(Res),
    String(String),
}

#[derive(Debug, Serialize)]
pub struct RoutingEventsResponse<Res>
where
    Res: Serialize + serde::de::DeserializeOwned + Clone,
{
    pub event: Option<routing_events::RoutingEvent>,
    pub response: Option<Res>,
}

impl<Res> RoutingEventsResponse<Res>
where
    Res: Serialize + serde::de::DeserializeOwned + Clone,
{
    pub fn new(event: Option<routing_events::RoutingEvent>, response: Option<Res>) -> Self {
        Self { event, response }
    }

    pub fn set_response(&mut self, response: Res) {
        self.response = Some(response);
    }

    pub fn set_event(&mut self, event: routing_events::RoutingEvent) {
        self.event = Some(event);
    }
}

impl<Req> RoutingEventsWrapper<Req>
where
    Req: Serialize + Clone,
{
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        tenant_id: id_type::TenantId,
        request_id: Option<RequestId>,
        payment_id: String,
        profile_id: id_type::ProfileId,
        merchant_id: id_type::MerchantId,
        flow: String,
        request: Option<Req>,
        parse_response: bool,
        log_event: bool,
    ) -> Self {
        Self {
            tenant_id,
            request_id,
            payment_id,
            profile_id,
            merchant_id,
            flow,
            request,
            parse_response,
            log_event,
            routing_event: None,
        }
    }

    pub fn construct_event_builder(
        self,
        url: String,
        routing_engine: routing_events::RoutingEngine,
        method: routing_events::ApiMethod,
    ) -> RoutingResult<Self> {
        let mut wrapper = self;
        let request = wrapper
            .request
            .clone()
            .ok_or(errors::RoutingError::RoutingEventsError {
                message: "Request body is missing".to_string(),
                status_code: 400,
            })?;

        let serialized_request = serde_json::to_value(&request)
            .change_context(errors::RoutingError::RoutingEventsError {
                message: "Failed to serialize RoutingRequest".to_string(),
                status_code: 500,
            })
            .attach_printable("Failed to serialize request body")?;

        let routing_event = routing_events::RoutingEvent::new(
            wrapper.tenant_id.clone(),
            "".to_string(),
            &wrapper.flow,
            serialized_request,
            url,
            method,
            wrapper.payment_id.clone(),
            wrapper.profile_id.clone(),
            wrapper.merchant_id.clone(),
            wrapper.request_id.clone(),
            routing_engine,
        );

        wrapper.set_routing_event(routing_event);

        Ok(wrapper)
    }

    pub async fn trigger_event<Res, F, Fut>(
        self,
        state: &SessionState,
        func: F,
    ) -> RoutingResult<RoutingEventsResponse<Res>>
    where
        F: FnOnce() -> Fut + Send,
        Res: Serialize + serde::de::DeserializeOwned + Clone,
        Fut: futures::Future<Output = RoutingResult<Option<Res>>> + Send,
    {
        let mut routing_event =
            self.routing_event
                .ok_or(errors::RoutingError::RoutingEventsError {
                    message: "Routing event is missing".to_string(),
                    status_code: 500,
                })?;

        let mut response = RoutingEventsResponse::new(None, None);

        let resp = func().await;
        match resp {
            Ok(ok_resp) => {
                if let Some(resp) = ok_resp {
                    routing_event.set_response_body(&resp);
                    // routing_event
                    //     .set_routable_connectors(ok_resp.get_routable_connectors().unwrap_or_default());
                    // routing_event.set_payment_connector(ok_resp.get_payment_connector());
                    routing_event.set_status_code(200);

                    response.set_response(resp.clone());
                    self.log_event
                        .then(|| state.event_handler().log_event(&routing_event));
                }
            }
            Err(err) => {
                // Need to figure out a generic way to log errors
                routing_event
                    .set_error(serde_json::json!({"error": err.current_context().to_string()}));

                match err.current_context() {
                    errors::RoutingError::RoutingEventsError { status_code, .. } => {
                        routing_event.set_status_code(*status_code);
                    }
                    _ => {
                        routing_event.set_status_code(500);
                    }
                }
                state.event_handler().log_event(&routing_event)
            }
        }

        response.set_event(routing_event);

        Ok(response)
    }

    pub fn set_log_event(&mut self, log_event: bool) {
        self.log_event = log_event;
    }

    pub fn set_request_body(&mut self, request: Req) {
        self.request = Some(request);
    }

    pub fn set_routing_event(&mut self, routing_event: routing_events::RoutingEvent) {
        self.routing_event = Some(routing_event);
    }
}

pub trait RoutingEventsInterface {
    fn get_routable_connectors(&self) -> Option<Vec<RoutableConnectorChoice>>;
    fn get_payment_connector(&self) -> Option<RoutableConnectorChoice>;
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct CalSuccessRateConfigEventRequest {
    pub min_aggregates_size: Option<u32>,
    pub default_success_rate: Option<f64>,
    pub specificity_level: api_routing::SuccessRateSpecificityLevel,
    pub exploration_percent: Option<f64>,
}

impl From<&api_routing::SuccessBasedRoutingConfigBody> for CalSuccessRateConfigEventRequest {
    fn from(value: &api_routing::SuccessBasedRoutingConfigBody) -> Self {
        Self {
            min_aggregates_size: value.min_aggregates_size,
            default_success_rate: value.default_success_rate,
            specificity_level: value.specificity_level,
            exploration_percent: value.exploration_percent,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct CalSuccessRateEventRequest {
    pub id: String,
    pub params: String,
    pub labels: Vec<String>,
    pub config: Option<CalSuccessRateConfigEventRequest>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct EliminationRoutingEventBucketConfig {
    pub bucket_size: Option<u64>,
    pub bucket_leak_interval_in_secs: Option<u64>,
}

impl From<&api_routing::EliminationAnalyserConfig> for EliminationRoutingEventBucketConfig {
    fn from(value: &api_routing::EliminationAnalyserConfig) -> Self {
        Self {
            bucket_size: value.bucket_size,
            bucket_leak_interval_in_secs: value.bucket_leak_interval_in_secs,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct EliminationRoutingEventRequest {
    pub id: String,
    pub params: String,
    pub labels: Vec<String>,
    pub config: Option<EliminationRoutingEventBucketConfig>,
}

/// API-1 types
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct CalContractScoreEventRequest {
    pub id: String,
    pub params: String,
    pub labels: Vec<String>,
    pub config: Option<api_routing::ContractBasedRoutingConfig>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct LabelWithScoreEventResponse {
    pub score: f64,
    pub label: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct CalSuccessRateEventResponse {
    pub labels_with_score: Vec<LabelWithScoreEventResponse>,
    pub routing_approach: RoutingApproach,
}

#[cfg(all(feature = "v1", feature = "dynamic_routing"))]
impl TryFrom<&ir_client::success_rate_client::CalSuccessRateResponse>
    for CalSuccessRateEventResponse
{
    type Error = errors::RoutingError;

    fn try_from(
        value: &ir_client::success_rate_client::CalSuccessRateResponse,
    ) -> Result<Self, Self::Error> {
        Ok(Self {
            labels_with_score: value
                .labels_with_score
                .iter()
                .map(|l| LabelWithScoreEventResponse {
                    score: l.score,
                    label: l.label.clone(),
                })
                .collect(),
            routing_approach: match value.routing_approach {
                0 => RoutingApproach::Exploration,
                1 => RoutingApproach::Exploitation,
                _ => {
                    return Err(errors::RoutingError::GenericNotFoundError {
                        field: "unknown routing approach from dynamic routing service".to_string(),
                    })
                }
            },
        })
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RoutingApproach {
    Exploitation,
    Exploration,
    Elimination,
    ContractBased,
    StaticRouting,
    Default,
}

impl RoutingApproach {
    pub fn from_decision_engine_approach(approach: &str) -> Self {
        match approach {
            "SR_SELECTION_V3_ROUTING" => Self::Exploitation,
            "SR_V3_HEDGING" => Self::Exploration,
            _ => Self::Default,
        }
    }
}

impl From<RoutingApproach> for common_enums::RoutingApproach {
    fn from(approach: RoutingApproach) -> Self {
        match approach {
            RoutingApproach::Exploitation => Self::SuccessRateExploitation,
            RoutingApproach::Exploration => Self::SuccessRateExploration,
            RoutingApproach::ContractBased => Self::ContractBasedRouting,
            RoutingApproach::StaticRouting => Self::RuleBasedRouting,
            _ => Self::DefaultFallback,
        }
    }
}

impl std::fmt::Display for RoutingApproach {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Exploitation => write!(f, "Exploitation"),
            Self::Exploration => write!(f, "Exploration"),
            Self::Elimination => write!(f, "Elimination"),
            Self::ContractBased => write!(f, "ContractBased"),
            Self::StaticRouting => write!(f, "StaticRouting"),
            Self::Default => write!(f, "Default"),
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct BucketInformationEventResponse {
    pub is_eliminated: bool,
    pub bucket_name: Vec<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct EliminationInformationEventResponse {
    pub entity: Option<BucketInformationEventResponse>,
    pub global: Option<BucketInformationEventResponse>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct LabelWithStatusEliminationEventResponse {
    pub label: String,
    pub elimination_information: Option<EliminationInformationEventResponse>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct EliminationEventResponse {
    pub labels_with_status: Vec<LabelWithStatusEliminationEventResponse>,
}

#[cfg(all(feature = "v1", feature = "dynamic_routing"))]
impl From<&ir_client::elimination_based_client::EliminationResponse> for EliminationEventResponse {
    fn from(value: &ir_client::elimination_based_client::EliminationResponse) -> Self {
        Self {
            labels_with_status: value
                .labels_with_status
                .iter()
                .map(
                    |label_with_status| LabelWithStatusEliminationEventResponse {
                        label: label_with_status.label.clone(),
                        elimination_information: label_with_status
                            .elimination_information
                            .as_ref()
                            .map(|info| EliminationInformationEventResponse {
                                entity: info.entity.as_ref().map(|entity_info| {
                                    BucketInformationEventResponse {
                                        is_eliminated: entity_info.is_eliminated,
                                        bucket_name: entity_info.bucket_name.clone(),
                                    }
                                }),
                                global: info.global.as_ref().map(|global_info| {
                                    BucketInformationEventResponse {
                                        is_eliminated: global_info.is_eliminated,
                                        bucket_name: global_info.bucket_name.clone(),
                                    }
                                }),
                            }),
                    },
                )
                .collect(),
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct ScoreDataEventResponse {
    pub score: f64,
    pub label: String,
    pub current_count: u64,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct CalContractScoreEventResponse {
    pub labels_with_score: Vec<ScoreDataEventResponse>,
}

#[cfg(all(feature = "v1", feature = "dynamic_routing"))]
impl From<&ir_client::contract_routing_client::CalContractScoreResponse>
    for CalContractScoreEventResponse
{
    fn from(value: &ir_client::contract_routing_client::CalContractScoreResponse) -> Self {
        Self {
            labels_with_score: value
                .labels_with_score
                .iter()
                .map(|label_with_score| ScoreDataEventResponse {
                    score: label_with_score.score,
                    label: label_with_score.label.clone(),
                    current_count: label_with_score.current_count,
                })
                .collect(),
        }
    }
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct CalGlobalSuccessRateConfigEventRequest {
    pub entity_min_aggregates_size: u32,
    pub entity_default_success_rate: f64,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct CalGlobalSuccessRateEventRequest {
    pub entity_id: String,
    pub entity_params: String,
    pub entity_labels: Vec<String>,
    pub global_labels: Vec<String>,
    pub config: Option<CalGlobalSuccessRateConfigEventRequest>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct UpdateSuccessRateWindowConfig {
    pub max_aggregates_size: Option<u32>,
    pub current_block_threshold: Option<api_routing::CurrentBlockThreshold>,
}

impl From<&api_routing::SuccessBasedRoutingConfigBody> for UpdateSuccessRateWindowConfig {
    fn from(value: &api_routing::SuccessBasedRoutingConfigBody) -> Self {
        Self {
            max_aggregates_size: value.max_aggregates_size,
            current_block_threshold: value.current_block_threshold.clone(),
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct UpdateLabelWithStatusEventRequest {
    pub label: String,
    pub status: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct UpdateSuccessRateWindowEventRequest {
    pub id: String,
    pub params: String,
    pub labels_with_status: Vec<UpdateLabelWithStatusEventRequest>,
    pub config: Option<UpdateSuccessRateWindowConfig>,
    pub global_labels_with_status: Vec<UpdateLabelWithStatusEventRequest>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct UpdateSuccessRateWindowEventResponse {
    pub status: UpdationStatusEventResponse,
}

#[cfg(all(feature = "v1", feature = "dynamic_routing"))]
impl TryFrom<&ir_client::success_rate_client::UpdateSuccessRateWindowResponse>
    for UpdateSuccessRateWindowEventResponse
{
    type Error = errors::RoutingError;

    fn try_from(
        value: &ir_client::success_rate_client::UpdateSuccessRateWindowResponse,
    ) -> Result<Self, Self::Error> {
        Ok(Self {
            status: match value.status {
                0 => UpdationStatusEventResponse::WindowUpdationSucceeded,
                1 => UpdationStatusEventResponse::WindowUpdationFailed,
                _ => {
                    return Err(errors::RoutingError::GenericNotFoundError {
                        field: "unknown updation status from dynamic routing service".to_string(),
                    })
                }
            },
        })
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum UpdationStatusEventResponse {
    WindowUpdationSucceeded,
    WindowUpdationFailed,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct LabelWithBucketNameEventRequest {
    pub label: String,
    pub bucket_name: String,
}

impl From<&api_routing::RoutableConnectorChoiceWithBucketName> for LabelWithBucketNameEventRequest {
    fn from(value: &api_routing::RoutableConnectorChoiceWithBucketName) -> Self {
        Self {
            label: value.routable_connector_choice.to_string(),
            bucket_name: value.bucket_name.clone(),
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct UpdateEliminationBucketEventRequest {
    pub id: String,
    pub params: String,
    pub labels_with_bucket_name: Vec<LabelWithBucketNameEventRequest>,
    pub config: Option<EliminationRoutingEventBucketConfig>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct UpdateEliminationBucketEventResponse {
    pub status: EliminationUpdationStatusEventResponse,
}

#[cfg(all(feature = "v1", feature = "dynamic_routing"))]
impl TryFrom<&ir_client::elimination_based_client::UpdateEliminationBucketResponse>
    for UpdateEliminationBucketEventResponse
{
    type Error = errors::RoutingError;

    fn try_from(
        value: &ir_client::elimination_based_client::UpdateEliminationBucketResponse,
    ) -> Result<Self, Self::Error> {
        Ok(Self {
            status: match value.status {
                0 => EliminationUpdationStatusEventResponse::BucketUpdationSucceeded,
                1 => EliminationUpdationStatusEventResponse::BucketUpdationFailed,
                _ => {
                    return Err(errors::RoutingError::GenericNotFoundError {
                        field: "unknown updation status from dynamic routing service".to_string(),
                    })
                }
            },
        })
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EliminationUpdationStatusEventResponse {
    BucketUpdationSucceeded,
    BucketUpdationFailed,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct ContractLabelInformationEventRequest {
    pub label: String,
    pub target_count: u64,
    pub target_time: u64,
    pub current_count: u64,
}

impl From<&api_routing::LabelInformation> for ContractLabelInformationEventRequest {
    fn from(value: &api_routing::LabelInformation) -> Self {
        Self {
            label: value.label.clone(),
            target_count: value.target_count,
            target_time: value.target_time,
            current_count: 1,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct UpdateContractRequestEventRequest {
    pub id: String,
    pub params: String,
    pub labels_information: Vec<ContractLabelInformationEventRequest>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct UpdateContractEventResponse {
    pub status: ContractUpdationStatusEventResponse,
}

#[cfg(all(feature = "v1", feature = "dynamic_routing"))]
impl TryFrom<&ir_client::contract_routing_client::UpdateContractResponse>
    for UpdateContractEventResponse
{
    type Error = errors::RoutingError;

    fn try_from(
        value: &ir_client::contract_routing_client::UpdateContractResponse,
    ) -> Result<Self, Self::Error> {
        Ok(Self {
            status: match value.status {
                0 => ContractUpdationStatusEventResponse::ContractUpdationSucceeded,
                1 => ContractUpdationStatusEventResponse::ContractUpdationFailed,
                _ => {
                    return Err(errors::RoutingError::GenericNotFoundError {
                        field: "unknown updation status from dynamic routing service".to_string(),
                    })
                }
            },
        })
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContractUpdationStatusEventResponse {
    ContractUpdationSucceeded,
    ContractUpdationFailed,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PreRoutingSkipRule {
    /// The payment method for which specific payment method types should skip pre-routing.
    /// IMPORTANT:
    /// - Do NOT add multiple entries with the same `payment_method`.
    /// - Each `payment_method` should appear **only once** in the config.
    ///
    /// Example:
    ///     {
    ///         bank_redirect: ["interac", "ach", "blik"]
    ///     }
    ///
    pub payment_method: common_enums::PaymentMethod,

    /// The list of payment method types under this payment method
    /// for which pre-routing must be skipped.
    ///
    /// This is a Vec because a single PM can have many PMTs
    /// that need to skip pre-routing:
    ///
    /// Example:
    ///     payment_method_types = ["interac", "ach", "blik"]
    ///
    pub payment_method_types: Vec<common_enums::PaymentMethodType>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MerchantPreRoutingConfig {
    pub skip_rules: Vec<PreRoutingSkipRule>,
}

/// Loads the merchant's pre-routing skip configuration and converts it into:
///     HashMap<PaymentMethod, HashSet<PaymentMethodType>>
///
/// Example output:
///     {
///         bank_redirect: { interac, ach, blik },
///         bank_debit: { sepa_debit }
///     }
pub async fn load_skip_pre_routing_config(
    state: &SessionState,
    pre_routing_disabled_pm_pmt_key: String,
) -> HashMap<enums::PaymentMethod, HashSet<enums::PaymentMethodType>> {
    let merchant_cfg = state
        .store
        .find_config_by_key_optional(&pre_routing_disabled_pm_pmt_key)
        .await
        .ok()
        .flatten()
        .and_then(|cfg| serde_json::from_str::<MerchantPreRoutingConfig>(&cfg.config).ok())
        .unwrap_or_default();

    let mut skip_map: HashMap<enums::PaymentMethod, HashSet<enums::PaymentMethodType>> =
        HashMap::new();

    for rule in merchant_cfg.skip_rules.iter() {
        skip_map
            .entry(rule.payment_method)
            .or_default()
            .extend(rule.payment_method_types.iter().copied());
    }

    skip_map
}

/// Returns `true` if pre-routing should be skipped for
/// the given (payment_method, payment_method_type) pair.
pub fn should_skip_prerouting(
    skip_map: &HashMap<enums::PaymentMethod, HashSet<enums::PaymentMethodType>>,
    pm: &enums::PaymentMethod,
    pmt: &enums::PaymentMethodType,
) -> bool {
    skip_map
        .get(pm)
        .map(|set| set.contains(pmt))
        .unwrap_or(false)
}

pub fn perform_pre_routing(
    allowed_pm_for_pre_routing: &LazyLock<HashSet<enums::PaymentMethod>>,
    allowed_pmt_for_pre_routing: &LazyLock<HashSet<enums::PaymentMethodType>>,
    payment_method: &enums::PaymentMethod,
    payment_method_type: &enums::PaymentMethodType,
    skip_map: &HashMap<enums::PaymentMethod, HashSet<enums::PaymentMethodType>>,
) -> bool {
    let should_skip_prerouting =
        should_skip_prerouting(skip_map, payment_method, payment_method_type);

    let pm_allowed = allowed_pm_for_pre_routing.contains(payment_method);
    let pmt_allowed = allowed_pmt_for_pre_routing.contains(payment_method_type);
    (pm_allowed || pmt_allowed) && !should_skip_prerouting
}

#[cfg(test)]
mod transform_de_output_tests {
    use super::*;

    fn mca(id: &str) -> id_type::MerchantConnectorAccountId {
        id_type::MerchantConnectorAccountId::wrap(id.to_string()).unwrap()
    }

    fn choice(connector: RoutableConnectors, id: &str) -> RoutableConnectorChoice {
        RoutableConnectorChoice {
            choice_kind: api_routing::RoutableChoiceKind::FullStruct,
            connector,
            merchant_connector_id: Some(mca(id)),
        }
    }

    fn info(name: &str, id: &str) -> ConnectorInfo {
        ConnectorInfo {
            gateway_name: name.to_string(),
            gateway_id: Some(id.to_string()),
        }
    }

    // The regression this PR fixes: two MCAs of the SAME connector must both survive.
    #[test]
    fn keeps_both_mcas_of_the_same_connector() {
        let out = transform_de_output_for_router(
            vec![
                info("paypal", "mca_9pE8yl5LFwGLe3fA2xNZ"),
                info("paypal", "mca_XLx05llokfkj8Tsb7nMa"),
            ],
            vec![choice(
                RoutableConnectors::Paypal,
                "mca_XLx05llokfkj8Tsb7nMa",
            )],
        )
        .unwrap();

        assert_eq!(out.len(), 2, "second paypal MCA was dropped");
        // volume-split winner stays at the front, loser follows as fallback
        let mca_order = out
            .iter()
            .map(|c| c.merchant_connector_id.clone())
            .collect::<Vec<_>>();
        assert_eq!(
            mca_order,
            vec![
                Some(mca("mca_XLx05llokfkj8Tsb7nMa")),
                Some(mca("mca_9pE8yl5LFwGLe3fA2xNZ")),
            ]
        );
    }

    // Guard against the opposite failure: a true duplicate must still collapse.
    #[test]
    fn still_dedups_exact_duplicates() {
        let out = transform_de_output_for_router(
            vec![
                info("paypal", "mca_9pE8yl5LFwGLe3fA2xNZ"),
                info("paypal", "mca_9pE8yl5LFwGLe3fA2xNZ"),
            ],
            vec![choice(
                RoutableConnectors::Paypal,
                "mca_9pE8yl5LFwGLe3fA2xNZ",
            )],
        )
        .unwrap();

        assert_eq!(out.len(), 1);
    }

    // Pre-existing behaviour for distinct connectors must be untouched.
    #[test]
    fn preserves_evaluated_first_ordering_for_distinct_connectors() {
        let out = transform_de_output_for_router(
            vec![info("adyen", "mca_ady"), info("stripe", "mca_strp")],
            vec![choice(RoutableConnectors::Stripe, "mca_strp")],
        )
        .unwrap();

        assert_eq!(out.len(), 2);
        let connector_order = out.iter().map(|c| c.connector).collect::<Vec<_>>();
        assert_eq!(
            connector_order,
            vec![RoutableConnectors::Stripe, RoutableConnectors::Adyen]
        );
    }
}

#[cfg(test)]
mod de_program_round_trip_tests {
    use super::*;

    fn mca_id(id: &str) -> id_type::MerchantConnectorAccountId {
        id_type::MerchantConnectorAccountId::wrap(id.to_string()).unwrap()
    }

    fn choice(connector: RoutableConnectors, id: &str) -> RoutableConnectorChoice {
        RoutableConnectorChoice {
            choice_kind: api_routing::RoutableChoiceKind::FullStruct,
            connector,
            merchant_connector_id: Some(mca_id(id)),
        }
    }

    fn comparison(lhs: &str, value: ast::ValueType) -> ast::Comparison {
        ast::Comparison {
            lhs: lhs.to_string(),
            comparison: ast::ComparisonType::Equal,
            value,
            metadata: HashMap::new(),
        }
    }

    /// A program exercising nesting and every value type euclid and the DE share.
    fn sample_program() -> ast::Program<ConnectorSelection> {
        ast::Program {
            default_selection: ConnectorSelection::Priority(vec![choice(
                RoutableConnectors::Stripe,
                "mca_default0000000000000",
            )]),
            rules: vec![ast::Rule {
                name: "cards_to_adyen".to_string(),
                connector_selection: ConnectorSelection::Priority(vec![
                    choice(RoutableConnectors::Adyen, "mca_adyen00000000000000000"),
                    choice(RoutableConnectors::Stripe, "mca_stripe0000000000000000"),
                ]),
                statements: vec![ast::IfStatement {
                    condition: vec![
                        comparison(
                            "payment_method",
                            ast::ValueType::EnumVariant("card".to_string()),
                        ),
                        comparison("amount", ast::ValueType::Number(MinorUnit::new(1000))),
                        comparison(
                            "currency",
                            ast::ValueType::EnumVariantArray(vec![
                                "USD".to_string(),
                                "EUR".to_string(),
                            ]),
                        ),
                        comparison(
                            "udf",
                            ast::ValueType::MetadataVariant(ast::MetadataValue {
                                key: "tier".to_string(),
                                value: "gold".to_string(),
                            }),
                        ),
                        comparison("label", ast::ValueType::StrValue("vip".to_string())),
                        comparison(
                            "amounts",
                            ast::ValueType::NumberArray(vec![MinorUnit::new(1), MinorUnit::new(2)]),
                        ),
                        comparison(
                            "band",
                            ast::ValueType::NumberComparisonArray(vec![ast::NumberComparison {
                                comparison_type: ast::ComparisonType::GreaterThan,
                                number: MinorUnit::new(500),
                            }]),
                        ),
                    ],
                    nested: Some(vec![ast::IfStatement {
                        condition: vec![comparison(
                            "card_network",
                            ast::ValueType::EnumVariant("visa".to_string()),
                        )],
                        nested: None,
                    }]),
                }],
            }],
            metadata: HashMap::new(),
        }
    }

    fn round_trip(program: ast::Program<ConnectorSelection>) -> ast::Program<ConnectorSelection> {
        let de: Program = program.try_into().expect("euclid -> DE failed");
        de.try_into().expect("DE -> euclid failed")
    }

    /// The property that lets the DE-authored rule be served as a normal typed
    /// response: converting out and back must not change the program.
    #[test]
    fn round_trip_is_lossless() {
        let original = sample_program();
        let expected = serde_json::to_value(&original).unwrap();
        let actual = serde_json::to_value(round_trip(original)).unwrap();
        assert_eq!(expected, actual);
    }

    #[test]
    fn round_trip_is_lossless_for_volume_split() {
        let original = ast::Program {
            default_selection: ConnectorSelection::VolumeSplit(vec![
                ConnectorVolumeSplit {
                    connector: choice(RoutableConnectors::Stripe, "mca_stripe0000000000000000"),
                    split: 70,
                },
                ConnectorVolumeSplit {
                    connector: choice(RoutableConnectors::Adyen, "mca_adyen00000000000000000"),
                    split: 30,
                },
            ]),
            rules: vec![],
            metadata: HashMap::new(),
        };
        let expected = serde_json::to_value(&original).unwrap();
        let actual = serde_json::to_value(round_trip(original)).unwrap();
        assert_eq!(expected, actual);
    }

    /// The DE can express a bare single connector; euclid cannot, so it widens to
    /// a one-element priority list, which routes identically.
    #[test]
    fn single_output_widens_to_priority_of_one() {
        let de = Program {
            globals: HashMap::new(),
            default_selection: Output::Single(ConnectorInfo::new(
                "stripe".to_string(),
                Some("mca_stripe0000000000000000".to_string()),
            )),
            rules: vec![],
            metadata: None,
        };
        let converted = ast::Program::try_from(de).expect("single output should convert");
        match converted.default_selection {
            ConnectorSelection::Priority(choices) => {
                assert_eq!(choices.len(), 1);
                assert_eq!(
                    choices.first().map(|c| c.connector.to_string()),
                    Some("stripe".to_string())
                );
            }
            other => panic!("expected priority, got {other:?}"),
        }
    }

    #[test]
    fn missing_metadata_becomes_empty_not_an_error() {
        let de = Program {
            globals: HashMap::new(),
            default_selection: Output::Priority(vec![ConnectorInfo::new(
                "stripe".to_string(),
                None,
            )]),
            rules: vec![],
            metadata: None,
        };
        assert!(ast::Program::try_from(de).unwrap().metadata.is_empty());
    }

    // Everything below is a DE construct euclid cannot represent. Each must fail
    // loudly rather than round-trip into a rule that routes differently.

    #[test]
    fn rejects_volume_split_priority_output() {
        let de = Program {
            globals: HashMap::new(),
            default_selection: Output::VolumeSplitPriority(vec![]),
            rules: vec![],
            metadata: None,
        };
        assert!(ast::Program::try_from(de).is_err());
    }

    #[test]
    fn rejects_global_ref_value() {
        assert!(convert_value_back(ValueType::GlobalRef("g".to_string())).is_err());
    }

    #[test]
    fn rejects_number_that_would_wrap_negative() {
        // u64::MAX as i64 would be -1, silently inverting the comparison.
        assert!(convert_value_back(ValueType::Number(u64::MAX)).is_err());
        let max_i64 = u64::try_from(i64::MAX).unwrap_or(u64::MAX);
        assert!(de_number_to_minor_unit(max_i64 + 1).is_err());
        assert_eq!(
            de_number_to_minor_unit(max_i64).unwrap(),
            MinorUnit::new(i64::MAX)
        );
    }

    #[test]
    fn rejects_unknown_connector_name() {
        let de = Program {
            globals: HashMap::new(),
            default_selection: Output::Priority(vec![ConnectorInfo::new(
                "not_a_real_connector".to_string(),
                None,
            )]),
            rules: vec![],
            metadata: None,
        };
        assert!(ast::Program::try_from(de).is_err());
    }
}