//! Payload module
//!
//! WASM payloads for pluggable request/response processing.
//!
//! Payloads can:
//! - Filter/modify requests before forwarding
//! - Filter/modify responses before returning
//! - Access request metadata (path, headers, method)
//! - Return early responses (block requests)
//! - Handle WebSocket events (ActionCable protocol)

#[cfg(feature = "wasm")]
mod runtime;
mod types;

#[cfg(feature = "wasm")]
pub use runtime::ModuleRuntime;
#[cfg(feature = "wasm")]
pub use runtime::{WsEventResult, WsEventType, WsPluginRuntime};
pub use types::{ModuleAction, RequestInfo, ResponseInfo};

// Stub ModuleRuntime when wasm feature is disabled
#[cfg(not(feature = "wasm"))]
pub struct ModuleRuntime;

#[cfg(not(feature = "wasm"))]
impl ModuleRuntime {
    pub fn new() -> Result<Self, anyhow::Error> {
        Ok(Self)
    }

    pub async fn load_modules(
        &self,
        configs: &[crate::charter::Module],
    ) -> Result<(), anyhow::Error> {
        if !configs.is_empty() {
            anyhow::bail!(
                "WASM modules are configured but this binary was built without the 'wasm' feature"
            );
        }
        Ok(())
    }

    pub async fn process_request(&self, _path: &str, _info: &RequestInfo) -> ModuleAction {
        ModuleAction::Continue
    }

    pub async fn process_response(&self, _path: &str, _info: &ResponseInfo) -> ModuleAction {
        ModuleAction::Continue
    }

    pub async fn module_names(&self) -> Vec<String> {
        Vec::new()
    }
}

#[cfg(not(feature = "wasm"))]
impl Default for ModuleRuntime {
    fn default() -> Self {
        Self
    }
}
