using System.ServiceModel.Syndication;
using Microsoft.AspNetCore.Mvc;
using zotanpw.Backend;
using zotanpw.Pages;

namespace zotanpw.Controllers;

[ApiController]
public class AtomFeed : Controller {
	private static readonly string AtomXml;

	static AtomFeed() {
		var feed = new SyndicationFeed {
			Id              = "https://zotan.pw/blog",
			Title           = new TextSyndicationContent("zotan.pw >> blog"),
			Description     = new TextSyndicationContent("Blog of a disabled neurodivergent queer person unhappy with the state of the world"),
			LastUpdatedTime = DateTimeOffset.Now,
			Authors         = { new SyndicationPerson("zotan@zotan.pw", "zotan", "https://zotan.pw") },
			Items = BlogModel.Posts.Take(10)
							 .Select(post => new SyndicationItem(post.Title, new TextSyndicationContent(post.Content, TextSyndicationContentKind.Html),
														         new Uri($"https://zotan.pw/blog/{post.Shorthand}"), $"https://zotan.pw/blog/{post.Shorthand}",
														         post.PublishedOn.ToDateTime(TimeOnly.MinValue)))
							 .ToList(),
			Generator = $"zotan.pw-web {Utils.Version}",
			ImageUrl  = new Uri("https://zotan.pw/favicon.ico"),
			Links     = { SyndicationLink.CreateSelfLink(new Uri("https://zotan.pw/blog/feed.atom")), SyndicationLink.CreateAlternateLink(new Uri("https://zotan.pw/blog")) }
		};

		AtomXml = new Atom10FeedFormatter(feed).ToXmlString();
	}

	[HttpGet, Route("/blog/feed.atom")]
	public ActionResult GetAtomFeed() {
		Response.ContentType = "application/atom+xml";
		return Content(AtomXml);
	}
}
