use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use validator::Validate;

/// 插件信息响应
#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
pub struct PluginResp {
    pub id: Uuid,
    pub name: String,
    pub version: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub author: Option<String>,
    pub status: String,
    pub config: serde_json::Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub installed_at: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enabled_at: Option<DateTime<Utc>>,
    pub entities: Vec<PluginEntityResp>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub permissions: Option<Vec<PluginPermissionResp>>,
    pub record_version: i32,
}

/// 插件实体信息
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
pub struct PluginEntityResp {
    pub name: String,
    pub display_name: String,
    pub table_name: String,
}

/// 插件权限信息
#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
pub struct PluginPermissionResp {
    pub code: String,
    pub name: String,
    pub description: String,
}

/// 插件健康检查响应
#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
pub struct PluginHealthResp {
    pub plugin_id: Uuid,
    pub status: String,
    pub details: serde_json::Value,
}

/// 更新插件配置请求
#[derive(Debug, Serialize, Deserialize, Validate, utoipa::ToSchema)]
pub struct UpdatePluginConfigReq {
    pub config: serde_json::Value,
    pub version: i32,
}

/// 插件列表查询参数
#[derive(Debug, Serialize, Deserialize, Validate, utoipa::IntoParams)]
pub struct PluginListParams {
    pub page: Option<u64>,
    pub page_size: Option<u64>,
    #[validate(length(max = 20, message = "状态值无效"))]
    pub status: Option<String>,
    #[validate(length(max = 100, message = "搜索关键词过长"))]
    pub search: Option<String>,
}
