using Microsoft.AspNetCore.Mvc;
using zotanpw.Pages;
using J = System.Text.Json.Serialization.JsonPropertyNameAttribute;

namespace zotanpw.Controllers;

[ApiController, Route("/blog/feed.json")]
public class JsonFeed : Controller {
	private static readonly JsonFeedObject Feedobj = new() {
		Items = BlogModel.Posts.Take(10)
						 .Select(post => new JsonFeedItem(post.Shorthand, $"https://zotan.pw/blog/{post.Shorthand}", post.Title, post.Content,
													      post.PublishedOn.ToDateTime(TimeOnly.MinValue).ToString("yyyy-MM-ddT00:00:00Z")))
						 .ToList()
	};

	[HttpGet]
	public JsonFeedObject Get() {
		Response.ContentType = "application/json";
		return Feedobj;
	}

	// Auto-generated JSON mapping
	// ReSharper disable All
	public class JsonFeedObject {
		[J("version")]       public string Version     => "https://jsonfeed.org/version/1.1";
		[J("language")]      public string Language    => "en";
		[J("title")]         public string Title       => "zotan.pw >> blog";
		[J("description")]   public string Description => "Blog of a disabled neurodivergent queer person unhappy with the state of the world";
		[J("home_page_url")] public string HomeUrl     => "https://zotan.pw/blog";
		[J("feed_url")]      public string FeedUrl     => "https://zotan.pw/blog/feed.json";
		[J("favicon")]       public string Favicon     => "https://zotan.pw/favicon.ico";

		[J("items")] public List<JsonFeedItem> Items { get; init; } = new();
	}

	public class JsonFeedItem {
		public JsonFeedItem(string id, string url, string title, string contentHtml, string datePublished) {
			_id            = id;
			_url           = url;
			_title         = title;
			_contentHtml   = contentHtml;
			_datePublished = datePublished;
		}

		[J("id")]             public string _id            { get; }
		[J("url")]            public string _url           { get; }
		[J("title")]          public string _title         { get; }
		[J("content_html")]   public string _contentHtml   { get; }
		[J("date_published")] public string _datePublished { get; }
	}
}
