use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Transport mode for MCP server
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum McpTransport {
    Stdio,
    Http,
}

impl std::fmt::Display for McpTransport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Stdio => write!(f, "stdio"),
            Self::Http => write!(f, "http"),
        }
    }
}

/// Status of a running MCP server
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum McpServerStatus {
    Running,
    Stopped,
    Error,
}

impl std::fmt::Display for McpServerStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Running => write!(f, "running"),
            Self::Stopped => write!(f, "stopped"),
            Self::Error => write!(f, "error"),
        }
    }
}

/// Configuration for a registered MCP server instance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpServerConfig {
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    pub id: Option<bson::oid::ObjectId>,
    /// Display name for this MCP server
    pub name: String,
    /// Endpoint URL (e.g. https://mcp.example.com/mcp)
    pub endpoint_url: String,
    /// Transport type
    pub transport: McpTransport,
    /// Port number (for HTTP transport)
    pub port: Option<u16>,
    /// Current status
    pub status: McpServerStatus,
    /// Bearer access token for authentication
    pub access_token: String,
    /// Which tools are enabled on this server
    pub tools_enabled: Vec<String>,
    /// Optional description / notes
    pub description: Option<String>,
    /// MongoDB URI this server connects to
    pub mongodb_uri: Option<String>,
    /// Database name
    pub mongodb_database: Option<String>,
    #[serde(with = "super::serde_helpers::bson_datetime")]
    pub created_at: DateTime<Utc>,
    #[serde(with = "super::serde_helpers::bson_datetime")]
    pub updated_at: DateTime<Utc>,
}
