//! 配置管理模块

mod theme;

pub use theme::*;

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    pub theme: ThemeConfig,
    pub reader: ReaderConfig,
    pub translation: TranslationConfig,
    pub storage: StorageConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReaderConfig {
    pub default_format: String,
    pub scroll_smooth: bool,
    pub show_toc: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TranslationConfig {
    pub provider: String,  // "google", "deepl", "ollama"
    pub api_key: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageConfig {
    pub library_path: String,
    pub cache_path: String,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            theme: ThemeConfig::default(),
            reader: ReaderConfig {
                default_format: "pdf".to_string(),
                scroll_smooth: true,
                show_toc: true,
            },
            translation: TranslationConfig {
                provider: "ollama".to_string(),
                api_key: None,
            },
            storage: StorageConfig {
                library_path: "./library".to_string(),
                cache_path: "./cache".to_string(),
            },
        }
    }
}

pub fn load() -> Config {
    // 从文件加载配置
    Config {
        theme: theme::load_config(),
        ..Config::default()
    }
}

pub fn save(config: &Config) -> anyhow::Result<()> {
    theme::save_config(&config.theme)?;
    Ok(())
}