use crate::Error;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

fn default_source_dir() -> PathBuf {
    PathBuf::from("content")
}

fn default_static_dir() -> PathBuf {
    PathBuf::from("static")
}

#[derive(Debug, Clone, Deserialize)]
pub struct Author {
    pub name: String,
    pub email: Option<String>,
    pub image: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Social {
    pub platform: String,
    pub url: String,
}

fn default_feed_limit() -> usize {
    20
}

fn default_posts_per_page() -> usize {
    10
}

fn default_wiki_default_category() -> String {
    "Other".to_string()
}

#[derive(Debug, Clone, Deserialize)]
pub struct Config {
    pub title: String,
    pub base_url: String,
    /// Short site description used as the Atom `<subtitle>` and RSS
    /// `<channel><description>`.
    pub description: Option<String>,
    #[serde(default = "default_source_dir")]
    pub source_dir: PathBuf,
    pub theme_dir: Option<PathBuf>,
    #[serde(default = "default_static_dir")]
    pub static_dir: PathBuf,
    #[serde(default)]
    pub authors: Vec<Author>,
    #[serde(default)]
    pub socials: Vec<Social>,
    /// Explicit ordering for wiki category headings. Categories listed here
    /// appear in this order; any wiki category not listed falls through to
    /// alphabetical placement after the listed ones, with uncategorised pages
    /// last.
    #[serde(default)]
    pub wiki_categories: Vec<String>,
    /// Display name used for wiki pages without a `category` in frontmatter.
    /// Surfaces both as the page's own category label and as the heading
    /// for the catch-all group on the wiki index. Defaults to `"Other"`.
    #[serde(default = "default_wiki_default_category")]
    pub wiki_default_category: String,
    /// Path to a source image (PNG, JPEG, SVG, etc.) used to generate
    /// favicons at standard sizes.
    pub favicon: Option<PathBuf>,
    /// Maximum number of blog posts included in RSS/Atom feeds. Set to `0`
    /// to include all posts. Defaults to 20.
    #[serde(default = "default_feed_limit")]
    pub feed_limit: usize,
    /// Maximum number of posts shown per page on the blog index and tag
    /// pages. Page 1 stays at the canonical URL (`/blog/`,
    /// `/tags/{tag}/`); subsequent pages live under `page/N/`. Defaults to 10.
    #[serde(default = "default_posts_per_page")]
    pub posts_per_page: usize,
}

impl std::str::FromStr for Config {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let config: Self = toml::from_str(s)?;
        config.validate()?;
        Ok(config)
    }
}

impl Config {
    fn validate(&self) -> Result<(), Error> {
        if !self.base_url.starts_with("http://") && !self.base_url.starts_with("https://") {
            return Err(Error::InvalidConfig {
                field: "base_url",
                message: format!(
                    "must start with http:// or https://, got {:?}",
                    self.base_url
                ),
            });
        }
        Ok(())
    }

    pub fn from_path(path: &Path) -> Result<Self, Error> {
        let text = std::fs::read_to_string(path)?;
        let mut config: Self = text.parse()?;

        // Resolve relative paths against the config file's parent directory
        // so that `aphid serve --config sub/aphid.toml` works from any CWD.
        let base = path
            .parent()
            .filter(|p| !p.as_os_str().is_empty())
            .unwrap_or(Path::new("."));
        config.resolve_paths(base);

        Ok(config)
    }

    pub fn normalized_base_url(&self) -> &str {
        Self::normalize_base_url(&self.base_url)
    }

    pub fn normalize_base_url(base_url: &str) -> &str {
        base_url.trim_end_matches('/')
    }

    fn resolve_path(path: &mut PathBuf, base: &Path) {
        if path.is_relative() {
            *path = base.join(&*path);
        }
    }

    fn resolve_optional_path(path: &mut Option<PathBuf>, base: &Path) {
        if let Some(path) = path {
            Self::resolve_path(path, base);
        }
    }

    /// Make all relative directory fields absolute by joining them with `base`.
    fn resolve_paths(&mut self, base: &Path) {
        Self::resolve_path(&mut self.source_dir, base);
        Self::resolve_path(&mut self.static_dir, base);
        Self::resolve_optional_path(&mut self.theme_dir, base);
        Self::resolve_optional_path(&mut self.favicon, base);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn minimal_config_applies_defaults() {
        let cfg: Config = r#"
            title = "My Site"
            base_url = "https://example.com"
            "#
        .parse()
        .unwrap();
        assert_eq!(cfg.title, "My Site");
        assert_eq!(cfg.base_url, "https://example.com");
        assert_eq!(cfg.source_dir, PathBuf::from("content"));
        assert!(cfg.theme_dir.is_none());
        assert_eq!(cfg.static_dir, PathBuf::from("static"));
        assert!(cfg.authors.is_empty());
        assert!(cfg.socials.is_empty());
        assert!(cfg.wiki_categories.is_empty());
        assert_eq!(cfg.wiki_default_category, "Other");
        assert_eq!(cfg.feed_limit, 20);
        assert_eq!(cfg.posts_per_page, 10);
    }

    #[test]
    fn fully_specified_config() {
        let cfg: Config = r#"
            title = "Full Site"
            base_url = "https://full.example.com"
            source_dir = "src_content"
            theme_dir = "tmpl"
            static_dir = "assets"
            feed_limit = 5
            posts_per_page = 3

            [[authors]]
            name = "Alice"
            email = "alice@example.com"

            [[authors]]
            name = "Bob"

            [[socials]]
            platform = "github"
            url = "https://github.com/example"
            "#
        .parse()
        .unwrap();
        assert_eq!(cfg.source_dir, PathBuf::from("src_content"));
        assert_eq!(cfg.theme_dir, Some(PathBuf::from("tmpl")));
        assert_eq!(cfg.static_dir, PathBuf::from("assets"));
        assert_eq!(cfg.authors.len(), 2);
        assert_eq!(cfg.authors[0].name, "Alice");
        assert_eq!(cfg.authors[0].email.as_deref(), Some("alice@example.com"));
        assert_eq!(cfg.authors[1].name, "Bob");
        assert!(cfg.authors[1].email.is_none());
        assert_eq!(cfg.socials.len(), 1);
        assert_eq!(cfg.socials[0].platform, "github");
        assert_eq!(cfg.feed_limit, 5);
        assert_eq!(cfg.posts_per_page, 3);
    }

    #[test]
    fn missing_title_is_error() {
        assert!(r#"base_url = "https://example.com""#.parse::<Config>().is_err());
    }

    #[test]
    fn missing_base_url_is_error() {
        assert!(r#"title = "My Site""#.parse::<Config>().is_err());
    }

    #[test]
    fn relative_base_url_is_error() {
        let result = r#"
            title = "My Site"
            base_url = "/"
            "#
        .parse::<Config>();
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("base_url"));
        assert!(err.contains("http"));
    }

    #[test]
    fn bare_path_base_url_is_error() {
        let result = r#"
            title = "My Site"
            base_url = "example.com"
            "#
        .parse::<Config>();
        assert!(result.is_err());
    }

    #[test]
    fn normalized_base_url_strips_trailing_slash() {
        let with_slash: Config = r#"
            title = "My Site"
            base_url = "https://example.com/"
            "#
        .parse()
        .unwrap();
        let without_slash: Config = r#"
            title = "My Site"
            base_url = "https://example.com"
            "#
        .parse()
        .unwrap();

        assert_eq!(with_slash.normalized_base_url(), "https://example.com");
        assert_eq!(without_slash.normalized_base_url(), "https://example.com");
    }
}
