use atom_syndication::{Category, Content, Entry, Feed, Generator, Link, Person, Text, TextType};

use super::{Article, Post};

pub async fn atom(posts: Vec<Post<Article>>) -> Feed {
    let me = Person {
        name: "cel".into(),
        email: Some("cel@blos.sm".into()),
        uri: Some("https://blos.sm".into()),
    };
    let mut authors = Vec::new();
    authors.push(me);
    let link = Link {
        href: "https://blos.sm/feed".into(),
        rel: "self".into(),
        hreflang: Some("en".into()),
        mime_type: Some("application/atom+xml".into()),
        title: Some("atom feed".into()),
        length: None,
    };
    let mut links = Vec::new();
    links.push(link);
    let mut feed = Feed {
        title: Text {
            value: "cel's site".into(),
            base: None,
            lang: Some("en".into()),
            r#type: TextType::Text,
        },
        id: "https://blos.sm".into(),
        updated: posts[0].created_at.into(),
        authors: authors.clone(),
        categories: Vec::new(),
        contributors: authors.clone(),
        generator: Some(Generator {
            value: "blos.sm".into(),
            uri: Some("https://bunny.garden/cel/blos.sm".into()),
            version: None,
        }),
        icon: Some("/icon.png".into()),
        links: links.clone(),
        logo: Some("/logo.png".into()),
        rights: None,
        subtitle: None,
        entries: Vec::new(),
        base: Some("https://blos.sm".into()),
        lang: Some("en".into()),
        ..Default::default()
    };
    for mut post in posts {
        post.render().await.unwrap_or_default();
        let mut id = String::from("https://blos.sm/blog/");
        id.push_str(&post.data.name);
        let categories = post
            .tags
            .into_iter()
            .map(|tag| Category {
                term: tag.clone(),
                scheme: None,
                label: Some(tag.clone()),
            })
            .collect();
        let entry = Entry {
            title: Text {
                value: post.subject.unwrap_or_default(),
                base: None,
                lang: Some("en".into()),
                r#type: TextType::Text,
            },
            id: id.clone(),
            updated: if let Some(updated_at) = post.updated_at {
                updated_at.into()
            } else {
                post.created_at.into()
            },
            authors: authors.clone(),
            categories,
            contributors: authors.clone(),
            links: links.clone(),
            published: Some(post.created_at.into()),
            rights: None,
            source: None,
            summary: None,
            content: Some(Content {
                base: None,
                lang: Some("en".into()),
                value: post.render,
                src: Some(id),
                content_type: Some("html".to_string()),
            }),
            ..Default::default()
        };
        feed.entries.push(entry);
    }
    feed
}
