Documentation
use crate::error::Error;
use crate::parser::ParseOptions;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;

/// Configuration for Markrust
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
    /// Parser configuration
    #[serde(default)]
    pub parser: ParserConfig,

    /// Renderer configuration
    #[serde(default)]
    pub renderer: RendererConfig,

    /// Theme configuration
    #[serde(default)]
    pub theme: ThemeConfig,

    /// Custom component directories
    #[serde(default)]
    pub components: Vec<String>,
}

/// Parser configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParserConfig {
    /// Enable GitHub-flavored markdown
    #[serde(default = "default_true")]
    pub gfm: bool,

    /// Enable smart punctuation
    #[serde(default = "default_true")]
    pub smart_punctuation: bool,

    /// Parse YAML frontmatter
    #[serde(default = "default_true")]
    pub frontmatter: bool,

    /// Enable custom component syntax
    #[serde(default = "default_true")]
    pub custom_components: bool,
}

/// Renderer configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RendererConfig {
    /// Default document title
    #[serde(default = "default_title")]
    pub title: String,

    /// Include default CSS
    #[serde(default = "default_true")]
    pub include_default_css: bool,

    /// Minify HTML output
    #[serde(default)]
    pub minify: bool,

    /// Generate table of contents
    #[serde(default)]
    pub toc: bool,

    /// Apply syntax highlighting to code blocks
    #[serde(default = "default_true")]
    pub syntax_highlight: bool,

    /// Add copy buttons to code blocks
    #[serde(default = "default_true")]
    pub code_copy_button: bool,

    /// Theme for syntax highlighting
    #[serde(default = "default_highlight_theme")]
    pub highlight_theme: String,
}

/// Theme configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThemeConfig {
    /// Theme name
    #[serde(default = "default_theme")]
    pub name: String,

    /// Theme variables
    #[serde(default)]
    pub variables: HashMap<String, String>,
}

impl Default for ParserConfig {
    fn default() -> Self {
        Self {
            gfm: true,
            smart_punctuation: true,
            frontmatter: true,
            custom_components: true,
        }
    }
}

impl Default for RendererConfig {
    fn default() -> Self {
        Self {
            title: default_title(),
            include_default_css: true,
            minify: false,
            toc: false,
            syntax_highlight: true,
            code_copy_button: true,
            highlight_theme: default_highlight_theme(),
        }
    }
}

impl Default for ThemeConfig {
    fn default() -> Self {
        Self {
            name: default_theme(),
            variables: HashMap::new(),
        }
    }
}

impl Config {
    /// Load configuration from a TOML file
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
        let content = fs::read_to_string(path)?;
        let config: Config = toml::from_str(&content)?;
        Ok(config)
    }
}

// Conversion from Config to component options
impl From<&Config> for ParseOptions {
    fn from(config: &Config) -> Self {
        ParseOptions {
            gfm: config.parser.gfm,
            smart_punctuation: config.parser.smart_punctuation,
            frontmatter: config.parser.frontmatter,
            custom_components: config.parser.custom_components,
        }
    }
}

fn default_true() -> bool {
    true
}

fn default_title() -> String {
    "Markrust Document".to_string()
}

fn default_theme() -> String {
    "modern".to_string()
}

fn default_highlight_theme() -> String {
    "InspiredGitHub".to_string()
}