use serde::{Serialize, Deserialize};
use std::collections::HashMap;
use utoipa::ToSchema;

// 单个启用的模型
#[derive(Serialize, Deserialize, Debug, Clone, ToSchema)]
pub struct LlmModel {
    pub model_id: String,           // e.g., "gpt-4o"
    pub name: Option<String>,       // 别名，用于UI显示
    pub is_active: bool,            // 是否在UI中可选
}

// 单个LLM供应商的完整配置
#[derive(Serialize, Deserialize, Debug, Clone, ToSchema)]
pub struct LlmProvider {
    pub name: String,               // "OpenAI 官方"
    pub api_base_url: String,
    pub api_key: String,            // 直接明文存储
    pub models: Vec<LlmModel>,      // 该供应商下我们启用的模型列表
}

// 整个LLM Provider注册中心的数据结构
pub type LlmProvidersConfig = HashMap<String, LlmProvider>; // Key: provider_id, e.g., "openai_official"

// --- Analysis Module Config (NEW TEMPLATE-BASED STRUCTURE) ---

/// Top-level configuration object for all analysis templates.
/// Key: Template ID (e.g., "standard_fundamentals")
pub type AnalysisTemplateSets = HashMap<String, AnalysisTemplateSet>;

/// A single, self-contained set of analysis modules representing a complete workflow.
/// e.g., "Standard Fundamental Analysis"
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, ToSchema)]
pub struct AnalysisTemplateSet {
    /// Human-readable name for the template set.
    pub name: String,
    /// All analysis modules contained within this template set.
    /// Key: Module ID (e.g., "fundamental_analysis")
    pub modules: HashMap<String, AnalysisModuleConfig>,
}

/// Configuration for a single analysis module.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, ToSchema)]
pub struct AnalysisModuleConfig {
    pub name: String,
    pub provider_id: String,
    pub model_id: String,
    pub prompt_template: String,
    /// List of dependencies. Each string must be a key in the parent `modules` HashMap.
    pub dependencies: Vec<String>,
}

// --- Analysis Module Config (OLD DEPRECATED STRUCTURE) ---

// This is the old, flat structure for analysis modules.
// It is DEPRECATED and will be removed once all services are migrated
// to the new AnalysisTemplateSets model.
pub type AnalysisModulesConfig = HashMap<String, OldAnalysisModuleConfig>;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct OldAnalysisModuleConfig {
    pub name: String,
    pub provider_id: String,
    pub model_id: String,
    pub prompt_template: String,
    pub dependencies: Vec<String>,
}


#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct SystemConfig {
    pub llm_providers: LlmProvidersConfig,
    pub analysis_modules: AnalysisModulesConfig,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum DataSourceProvider {
    Tushare,
    Finnhub,
    Alphavantage,
    Yfinance,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, ToSchema)]
pub struct DataSourceConfig {
    pub provider: DataSourceProvider,
    pub api_key: Option<String>,
    pub api_url: Option<String>,
    pub enabled: bool,
}

// 数据源配置集合（集中、强类型、单一来源）
pub type DataSourcesConfig = HashMap<String, DataSourceConfig>;
