//! Generated cTrader Open API protobuf types and dispatch helpers.
//!
//! All types are generated by prost from the 4 vendored `.proto` files.
//! The `PayloadType` constants and dispatch helpers below map the
//! `payloadType` discriminator in `ProtoMessage` to concrete message types.

// Include prost-generated code. The proto files don't declare a `package`,
// so prost emits everything into the root (unnamed) module.
include!(concat!(env!("OUT_DIR"), "/_.rs"));

// ── Payload-type constants ──────────────────────────────────────────
// Common
pub const PT_HEARTBEAT_EVENT: u32 = 51;
pub const PT_ERROR_RES: u32 = 50;

// Application / Account auth
pub const PT_APP_AUTH_REQ: u32 = 2100;
pub const PT_APP_AUTH_RES: u32 = 2101;
pub const PT_ACCOUNT_AUTH_REQ: u32 = 2102;
pub const PT_ACCOUNT_AUTH_RES: u32 = 2103;

// Version
pub const PT_VERSION_REQ: u32 = 2104;
pub const PT_VERSION_RES: u32 = 2105;

// Orders
pub const PT_NEW_ORDER_REQ: u32 = 2106;
pub const PT_CANCEL_ORDER_REQ: u32 = 2108;
pub const PT_AMEND_ORDER_REQ: u32 = 2109;
pub const PT_CLOSE_POSITION_REQ: u32 = 2111;

// Symbols
pub const PT_SYMBOLS_LIST_REQ: u32 = 2114;
pub const PT_SYMBOLS_LIST_RES: u32 = 2115;
pub const PT_SYMBOL_BY_ID_REQ: u32 = 2116;
pub const PT_SYMBOL_BY_ID_RES: u32 = 2117;

// Account
pub const PT_TRADER_REQ: u32 = 2121;
pub const PT_TRADER_RES: u32 = 2122;
pub const PT_RECONCILE_REQ: u32 = 2124;
pub const PT_RECONCILE_RES: u32 = 2125;

// Execution & errors
pub const PT_EXECUTION_EVENT: u32 = 2126;
pub const PT_ORDER_ERROR_EVENT: u32 = 2132;

// Spot subscriptions
pub const PT_SUBSCRIBE_SPOTS_REQ: u32 = 2127;
pub const PT_SUBSCRIBE_SPOTS_RES: u32 = 2128;
pub const PT_UNSUBSCRIBE_SPOTS_REQ: u32 = 2129;
pub const PT_UNSUBSCRIBE_SPOTS_RES: u32 = 2130;
pub const PT_SPOT_EVENT: u32 = 2131;

// OA error
pub const PT_OA_ERROR_RES: u32 = 2142;

// Accounts by token
pub const PT_GET_ACCOUNTS_REQ: u32 = 2149;
pub const PT_GET_ACCOUNTS_RES: u32 = 2150;

// Disconnect / invalidation events
pub const PT_CLIENT_DISCONNECT_EVENT: u32 = 2148;
pub const PT_ACCOUNTS_TOKEN_INVALIDATED: u32 = 2147;
pub const PT_ACCOUNT_DISCONNECT_EVENT: u32 = 2164;

/// Categorize a `payloadType` for logging and dispatch.
pub const fn payload_type_name(pt: u32) -> &'static str {
    match pt {
        PT_HEARTBEAT_EVENT => "HeartbeatEvent",
        PT_ERROR_RES => "ErrorRes",
        PT_APP_AUTH_REQ => "ApplicationAuthReq",
        PT_APP_AUTH_RES => "ApplicationAuthRes",
        PT_ACCOUNT_AUTH_REQ => "AccountAuthReq",
        PT_ACCOUNT_AUTH_RES => "AccountAuthRes",
        PT_VERSION_REQ => "VersionReq",
        PT_VERSION_RES => "VersionRes",
        PT_NEW_ORDER_REQ => "NewOrderReq",
        PT_CANCEL_ORDER_REQ => "CancelOrderReq",
        PT_AMEND_ORDER_REQ => "AmendOrderReq",
        PT_CLOSE_POSITION_REQ => "ClosePositionReq",
        PT_SYMBOLS_LIST_REQ => "SymbolsListReq",
        PT_SYMBOLS_LIST_RES => "SymbolsListRes",
        PT_SYMBOL_BY_ID_REQ => "SymbolByIdReq",
        PT_SYMBOL_BY_ID_RES => "SymbolByIdRes",
        PT_TRADER_REQ => "TraderReq",
        PT_TRADER_RES => "TraderRes",
        PT_RECONCILE_REQ => "ReconcileReq",
        PT_RECONCILE_RES => "ReconcileRes",
        PT_EXECUTION_EVENT => "ExecutionEvent",
        PT_ORDER_ERROR_EVENT => "OrderErrorEvent",
        PT_SUBSCRIBE_SPOTS_REQ => "SubscribeSpotsReq",
        PT_SUBSCRIBE_SPOTS_RES => "SubscribeSpotsRes",
        PT_UNSUBSCRIBE_SPOTS_REQ => "UnsubscribeSpotsReq",
        PT_UNSUBSCRIBE_SPOTS_RES => "UnsubscribeSpotsRes",
        PT_SPOT_EVENT => "SpotEvent",
        PT_OA_ERROR_RES => "OAErrorRes",
        PT_GET_ACCOUNTS_REQ => "GetAccountsReq",
        PT_GET_ACCOUNTS_RES => "GetAccountsRes",
        PT_CLIENT_DISCONNECT_EVENT => "ClientDisconnectEvent",
        PT_ACCOUNTS_TOKEN_INVALIDATED => "AccountsTokenInvalidated",
        PT_ACCOUNT_DISCONNECT_EVENT => "AccountDisconnectEvent",
        _ => "Unknown",
    }
}

/// Returns `true` if the payload type is a server-initiated event
/// (i.e. not a direct response to a client request).
pub const fn is_event(pt: u32) -> bool {
    matches!(
        pt,
        PT_HEARTBEAT_EVENT
            | PT_EXECUTION_EVENT
            | PT_SPOT_EVENT
            | PT_ORDER_ERROR_EVENT
            | PT_CLIENT_DISCONNECT_EVENT
            | PT_ACCOUNTS_TOKEN_INVALIDATED
            | PT_ACCOUNT_DISCONNECT_EVENT
    )
}
