//! LLM Gateway — Unified Large Language Model Interface (Phase-78 ~ Phase-81)
//!
//! Provides a unified abstraction layer for interacting with multiple LLM providers
//! (OpenAI, DeepSeek, Ollama, Gemini, Anthropic, etc.) through a single API.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────┐
//! │              LLM Gateway (Rust Core)             │
//! │  ┌────────────────┐ ┌────────────────────────┐  │
//! │  │ Provider Router │ │  Conversation Manager  │  │
//! │  │ (route/failover)│ │  (CRUD + history)      │  │
//! │  └───────┬────────┘ └───────────┬────────────┘  │
//! │  ┌───────┴────────────────────────────────────┐  │
//! │  │           Unified LLM Client               │  │
//! │  │  OpenAI-compat │ Gemini │ Anthropic │ Local │  │
//! │  └────────────────────────────────────────────┘  │
//! │  ┌────────────────────────────────────────────┐  │
//! │  │     Stream Engine (SSE / gRPC Stream)      │  │
//! │  └────────────────────────────────────────────┘  │
//! └─────────────────────────────────────────────────┘
//! ```
//!
//! # Modules
//!
//! - `types`: Data models — Provider, Model, StreamEvent, ChatConfig, etc.
//! - `error`: Error types — `LlmError` and `LlmResult`
//! - `client`: Unified `LlmClient` trait + `LlmMessage` types
//! - `store`: Provider & Model CRUD storage (DashMap + JSON file)
//! - `openai_client`: OpenAI-compatible client (covers 80%+ of providers)
//! - `gemini_client`: Google Gemini native protocol client
//! - `anthropic_client`: Anthropic Messages API client
//! - `router`: Provider routing, failover, health monitoring
//! - `factory`: Client factory (creates clients from provider config)
//!
//! # Feature Flags
//!
//! - `llm-gateway`: Enables the LLM Gateway module (types + traits + store + router)
//! - `http-client`: Required for external API calls (reqwest-based clients)

// ── Core modules (always available with llm-gateway) ──

pub mod types;
pub mod error;
pub mod client;
pub mod store;
pub mod router;
pub mod factory;
pub mod conversation;
pub mod conversation_manager;
pub mod stream_engine;
pub mod prompt_template;
pub mod tool_executor;
pub mod tool_call_loop;
pub mod memory;
pub mod nl_to_sql;
pub mod agent;
#[cfg(feature = "federation")]
pub mod builtin_tools;

// ── Provider-specific clients (require http-client feature) ──

#[cfg(feature = "http-client")]
pub mod openai_client;
#[cfg(feature = "http-client")]
pub mod gemini_client;
#[cfg(feature = "http-client")]
pub mod anthropic_client;

// ── Re-exports ──

// Re-export core types
pub use types::{
    // Provider types
    LlmProvider, ProviderBrand, ProviderProtocol, ProviderStatus,
    ProviderCapabilities, RateLimit,
    // Model types
    LlmModel, ModelStatus, ModelCapabilities, ModelPricing,
    ModelDefaultConfig,
    // Chat types
    ChatConfig, ChatResponse, ToolDefinition, FunctionDefinition,
    ToolCall, TokenUsage, FinishReason,
    // Stream types
    StreamEvent, AudioFormat,
};

// Re-export error types
pub use error::{LlmError, LlmResult};

// Re-export client types
pub use client::{LlmClient, LlmMessage, MessageRole, ContentPart};

// Re-export store
pub use store::LlmStore;

// Re-export router
pub use router::{ProviderRouter, RoutingStrategy};

// Re-export factory
pub use factory::LlmClientFactory;

// Re-export conversation types (Phase-79)
pub use conversation::{
    LlmConversation, ConversationConfig, StoredMessage,
    Attachment, MessageStatus, ConversationQuery, MessageQuery, UsageStats,
};
pub use conversation_manager::ConversationManager;

// Re-export stream engine (Phase-80)
pub use stream_engine::{SseEvent, stream_event_to_sse, parse_sse_event};

// Re-export prompt template (Phase-81)
pub use prompt_template::{PromptTemplate, PromptTemplateStore};

// Re-export tool executor (Phase-83)
pub use tool_executor::{ToolExecutor, ToolMetadata, ToolRegistry, ToolResult};

// Re-export tool call loop (Phase-83)
pub use tool_call_loop::{ToolCallLoop, ToolCallLoopResult, ToolCallRound, ToolCallResult};

// Re-export memory (Phase-84)
pub use memory::{MemoryManager, MemoryConfig, MemoryContext, MemoryLayer, InMemoryLayer};

// Re-export NL-to-SQL engine (Phase-85)
pub use nl_to_sql::{
    NlToSqlEngine, NlToSqlConfig, NlQueryResult, SqlAttempt,
    SchemaContext, TableInfo, ColumnInfo,
    SqlSafetyResult, validate_sql_safety,
    extract_sql_from_response, ensure_limit,
};

// Re-export Agent engine (Phase-87)
pub use agent::{
    AgentEngine, AgentConfig, AgentResult, AgentStep, AgentStepType,
    AgentPlan, PlanStep, AgentContext, AgentToolResult,
    AgentTemplate, AgentTemplateConfig, AgentResourceLimits, AgentResourceUsage,
    get_builtin_template, list_builtin_templates, apply_template,
};

// Re-export provider clients (when http-client is enabled)
#[cfg(feature = "http-client")]
pub use openai_client::OpenAiCompatClient;
#[cfg(feature = "http-client")]
pub use gemini_client::GeminiClient;
#[cfg(feature = "http-client")]
pub use anthropic_client::AnthropicClient;
