use crate::kind::DecisionListKind;
use crate::{DecisionEntryType, OrderId};
use std::fmt::Display;

pub enum Endpoints {
    FetchOrderV1(OrderId),
    CreateOrderV1,
    FetchDecisionListEntryV1(DecisionListKind, DecisionEntryType, String),
    CreateDecisionListEntryV1(DecisionListKind, DecisionEntryType),
    UpdateDecisionListEntryV1(DecisionListKind, DecisionEntryType, String),
    DeleteDecisionListEntryV1(DecisionListKind, DecisionEntryType, String),
}

impl Endpoints {
    /// Formata uma URL de decision list com valor codificado
    ///
    /// Helper interno para evitar duplicação de lógica de encoding de URLs
    fn format_decision_list_with_value(
        kind: &DecisionListKind,
        entry_type: &DecisionEntryType,
        entry_value: &str,
    ) -> String {
        let encoded_value = urlencoding::encode(entry_value);
        format!("v1/{kind}/{entry_type}/{encoded_value}")
    }
}

impl Display for Endpoints {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let str = match self {
            Endpoints::FetchOrderV1(order_id) => format!("v1/orders/{order_id}"),
            Endpoints::CreateOrderV1 => "v1/orders".to_string(),
            Endpoints::FetchDecisionListEntryV1(kind, entry_type, entry_value)
            | Endpoints::UpdateDecisionListEntryV1(kind, entry_type, entry_value)
            | Endpoints::DeleteDecisionListEntryV1(kind, entry_type, entry_value) => {
                Self::format_decision_list_with_value(kind, entry_type, entry_value)
            }
            Endpoints::CreateDecisionListEntryV1(kind, entry_type) => {
                format!("v1/{kind}/{entry_type}")
            }
        };
        write!(f, "{}", str)
    }
}
