import { getCollection } from "astro:content";
import { Feed } from "feed";

import { compareDesc } from "date-fns"
import { toZonedTime } from 'date-fns-tz'

import sfjson from '../data/sharefeed.json'
import type { ShareFeedEntry } from "../lib/utils";

export const baseURL = "https://eleboog.com"
export const author = {
    name: "Kebo Kitanari",
    email: "kebokyo@eleboog.com",
    link: baseURL
};

import { timeZone } from "./utils";

export async function computeFeed() {
    const mainFeed = new Feed({
        title: "eleboog.com feed",
        description: "The feed for Kebo Kitanari's writing and other content. Contains blog posts, sharefeed entries, and more.",
        id: baseURL,
        link: baseURL,
        language: "en",
        copyright: "All Rights Reserved 2024 Kebo Kitanari",
        feedLinks: {
            atom: `${baseURL}/feeds/feed.xml`,
            json: `${baseURL}/feeds/feed.json`
        },
        author
    })

    await addPostsToFeed(mainFeed, true);
    addSharefeedEntriesToFeed(mainFeed, true);

    return mainFeed;
}

export function addSharefeedEntriesToFeed(feed: Feed, main?: boolean) {
    const entries = sfjson.sharefeed
        .sort((a:ShareFeedEntry, b:ShareFeedEntry) => compareDesc(new Date(a.date), new Date(b.date))) 
        .filter((entry:ShareFeedEntry) => !(entry.draft))
        .slice(0,15);
    
    entries.forEach((entry: ShareFeedEntry) => {
        let title: string = '';
        if (main) {
            /*if (compareDesc(new Date(toZonedTime(entry.date)), new Date("2024-07-13")) == 1) {
                return;
            }*/
            title = title.concat('[sf] ');
        }
        if (entry.author) {
            title = title.concat(`${entry.author}: `);
        }
        title = title.concat(entry.title);

        feed.addItem({
            title: title,
            id: entry.url,
            link: entry.url,
            description: entry.note,
            content: entry.note,
            author: [{
                name: entry.author
            }],
            date: toZonedTime(entry.date, timeZone)
        })
    })
}

export async function addPostsToFeed(feed: Feed, main?: boolean) {
    const posts = await getCollection('posts')

    posts.sort((a, b) => compareDesc(new Date(a.data.date), new Date(b.data.date)))
        .filter((post) => !post.data.draft)
        .slice(0,20);

    posts.forEach((post) => {
        if (post.data.draft) { return }

        const url: string = `${baseURL}/posts/${post.slug}`
        let title: string = '';
        if (main) {
            title = title.concat('[blog] ');
        }
        title = title.concat(post.data.title);

        feed.addItem({
            title: title,
            id: url,
            link: url,
            description: post.data.summary ?? "Whoops, I didn't write a summary for this. Yell at me that I need to do so.",
            content: post.data.summary ?? "Whoops, I didn't write a summary for this. Yell at me that I need to do so.",
            author: [author],
            date: toZonedTime(post.data.date, timeZone),
        });
    })
}

// stolen from Jordan Webb - https://github.com/jordemort/jordemort.github.io/blob/main/src/utils/makeFeed.ts
export function injectXSL(xml: string, xsl: string) {
  let i = xml.indexOf('?>');
  let before = xml.slice(0, i + 2);
  let after = xml.slice(i + 2);

  return before + `<?xml-stylesheet href="${xsl}" type="text/xsl"?>` + after;
}