//
// Copyright (C) 2024 - 2025 Automated Design Corp. All Rights Reserved.
//
// IPC Module for CommandMessage-based communication between autocore-server
// and external modules. Provides a unified interface for both internal and
// external module development.
//

//! # IPC Module
//!
//! This module provides infrastructure for CommandMessage-based IPC communication
//! between autocore-server and external modules over TCP sockets.
//!
//! ## Key Components
//!
//! - [`CommandMessage`] - Unified message type for all IPC communication
//! - [`MessageType`] - Enum defining the semantic meaning of messages
//! - [`ModuleHandler`] - Trait that external modules implement (analogous to AsyncServelet)
//! - [`IpcClient`] - Client for external modules to connect to autocore-server
//! - [`IpcServer`] - Server-side listener for incoming module connections
//! - [`TopicRouter`] - Helper for routing messages to callbacks based on topic patterns
//!
//! ## Example: External Module
//!
//! ```ignore
//! use mechutil::ipc::{ModuleHandler, IpcClient, CommandMessage, MessageType};
//! use async_trait::async_trait;
//!
//! struct MyModule {
//!     // module state
//! }
//!
//! #[async_trait]
//! impl ModuleHandler for MyModule {
//!     async fn handle_message(&mut self, msg: CommandMessage) -> CommandMessage {
//!         // Process the message and return response
//!         msg.into_response(serde_json::json!({"status": "ok"}))
//!     }
//!
//!     async fn on_initialize(&mut self) -> Result<(), anyhow::Error> {
//!         Ok(())
//!     }
//!
//!     async fn on_finalize(&mut self) -> Result<(), anyhow::Error> {
//!         Ok(())
//!     }
//!
//!     fn domain(&self) -> &str {
//!         "mymodule"
//!     }
//! }
//!
//! #[tokio::main]
//! async fn main() {
//!     let module = MyModule {};
//!     let client = IpcClient::connect("127.0.0.1:9100", module).await.unwrap();
//!     client.run().await;
//! }
//! ```

mod command_message;
mod message;
mod handler;
mod transport;
mod client;
mod server;
mod router;
mod error;
mod command_def;
mod args;
pub mod schema;

// Core message types - CommandMessage is the primary type
pub use command_message::{
    CommandMessage,
    MessageType,
    next_transaction_id,
    current_timecode,
};

// Legacy types (deprecated) - kept for backward compatibility during transition
#[allow(deprecated)]
pub use command_message::{Action, CommandMessageResult};

// Wire format and control types
pub use message::{IpcMessageHeader, ControlType, ModuleRegistration};

// Handler trait
pub use handler::{ModuleHandler, ModuleHandlerExt, BaseModuleHandler};

// Schema publishing
pub use schema::{SchemaEnvelope, build_envelope};

// Transport layer
pub use transport::{IpcTransport, FramedStream, SplitTransport};

// Client (for external modules)
pub use client::{IpcClient, IpcClientConfig, IpcClientBuilder, BroadcastSender};

// Server (for autocore-server)
pub use server::{IpcServer, IpcServerConfig, ModuleConnection, ServerEvent, handle_module_connection};

// Routing
pub use router::{TopicRouter, TopicRouterBuilder, RouteHandler, RouteMatch, RouteContext};

// CLI argument parsing
pub use args::ModuleArgs;

// Errors
pub use error::IpcError;

// Command definitions
pub use command_def::{ArgType, ArgDef, CommandDef, ParsedArgs, CatalogEntry, CommandRegistry};
