---
import type { GetStaticPaths, Page } from "astro";
import { Image } from "astro:assets";
import { getCollection, getEntries, type CollectionEntry } from "astro:content";
import GalleryLayout from "@layouts/GalleryLayout.astro";
import { t } from "@i18n";
import UserComponent from "@components/UserComponent.astro";
import { IconNoOneUnder18, IconSquareRSS } from "@components/icons";

type StoryWithPubDate = CollectionEntry<"stories"> & { data: { pubDate: Date } };

type Props = {
  page: Page<StoryWithPubDate & { authors: CollectionEntry<"users">[] }>;
};

export const getStaticPaths: GetStaticPaths = async ({ paginate }) => {
  const stories = await Promise.all(
    ((await getCollection("stories", (story) => !story.data.isDraft && story.data.pubDate)) as StoryWithPubDate[])
      .sort((a, b) => b.data.pubDate.getTime() - a.data.pubDate.getTime())
      .map(async (story) => ({
        ...story,
        authors: await getEntries(story.data.authors),
      })),
  );
  return paginate(stories, { pageSize: 30 }) satisfies { props: Props }[];
};

const { page } = Astro.props;
const totalPages = Math.ceil(page.total / page.size);
---

<GalleryLayout pageTitle="Stories" class="h-feed">
  <Fragment slot="head">
    <meta property="og:description" content="Bad Manners || Stories written by yours truly." />
    <meta name="description" content="Safe vore stories written by Bad Manners." />
  </Fragment>
  <h1 class="p-name m-2 grow text-3xl font-semibold text-stone-800 dark:text-stone-100">Stories</h1>
  <hr class="mb-3 ml-[2px] mt-2 h-[4px] max-w-xs rounded-sm bg-stone-800 dark:bg-stone-100" aria-hidden="true" />
  <div class="my-4 flex">
    <p class="p-summary grow">The bulk of my content!</p>
    <a class="u-url text-link ml-2 p-1 md:mr-5" href="/stories/feed.xml" rel="alternate" title="RSS feed" data-tooltip>
      <IconSquareRSS width="2rem" height="2rem" />
      <span class="sr-only">RSS feed</span>
    </a>
  </div>
  <p class="text-center font-light text-stone-950 dark:text-white">
    {
      page.start === page.end
        ? `Displaying story #${page.start + 1}`
        : `Displaying stories #${page.start + 1}–${page.end + 1}`
    } / {page.total}
  </p>
  <div class="mx-auto mb-6 mt-2 flex w-fit rounded-lg border border-stone-400 dark:border-stone-500">
    {
      [...Array(totalPages).keys()]
        .map((p) => p + 1)
        .map((p) =>
          p === page.currentPage ? (
            <span
              class:list={[
                p === 1 || "border-l",
                "border-stone-400 px-4 py-1 font-semibold text-stone-900 dark:border-stone-500 dark:text-stone-50",
              ]}
            >
              {p}
            </span>
          ) : (
            <a
              class:list={[
                p === 1 || "border-l",
                "text-link border-stone-400 px-2 py-1 underline dark:border-stone-500",
              ]}
              href={p === 1 ? "/stories" : `/stories/${p}`}
            >
              {p}
            </a>
          ),
        )
    }
  </div>
  <ul class="flex flex-wrap items-start justify-center gap-4 text-center md:justify-normal">
    {
      page.data.map((story, i) => (
        <li class="h-entry break-inside-avoid">
          <a
            class="u-url text-link hover:underline focus:underline"
            href={`/stories/${story.id}`}
            title={t(story.data.lang, "story/warnings", story.data.wordCount, story.data.contentWarning)}
            data-tooltip
          >
            {story.data.thumbnail ? (
              <div class="flex aspect-square max-w-[192px] justify-center">
                <Image
                  loading={i < 10 ? "eager" : "lazy"}
                  class="u-photo m-auto"
                  src={story.data.thumbnail}
                  alt={`Thumbnail for ${story.data.title}`}
                  width={192}
                />
              </div>
            ) : null}
            <div class="max-w-[192px] text-sm">
              <span class="p-name" aria-label="Title">
                {story.data.title}
              </span>
              <br />
              {story.data.isAgeRestricted ? (
                <span class="inline-block align-middle" aria-label="Age restricted">
                  <IconNoOneUnder18 width="1.25rem" height="1.25rem" />
                </span>
              ) : null}
              <time
                class="dt-published italic"
                datetime={story.data.pubDate.toISOString().slice(0, 10)}
                aria-label="Publish date"
              >
                {story.data.pubDate.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" })}
              </time>
            </div>
          </a>
          <div class="sr-only select-none">
            <p class="p-category" aria-label="Category">
              Story
            </p>
            <p class="p-summary" aria-label="Summary">
              {t(story.data.lang, "story/warnings", story.data.wordCount, story.data.contentWarning)}
            </p>
            <p class="p-language" aria-label="Language">
              {t(story.data.lang, "language/language")}
            </p>
            <div aria-label="Authors">
              <span>{story.authors.length == 1 ? "Author:" : "Authors:"}</span>
              {story.authors.map((author) => (
                <UserComponent rel="author" class="p-author" user={author} lang={story.data.lang} />
              ))}
            </div>
          </div>
        </li>
      ))
    }
  </ul>
  <div class="mx-auto my-6 flex w-fit rounded-lg border border-stone-400 dark:border-stone-500">
    {
      [...Array(totalPages).keys()]
        .map((p) => p + 1)
        .map((p) =>
          p === page.currentPage ? (
            <span
              class:list={[
                p === 1 || "border-l",
                "border-stone-400 px-4 py-1 font-semibold text-stone-900 dark:border-stone-500 dark:text-stone-50",
              ]}
            >
              {p}
            </span>
          ) : (
            <a
              class:list={[
                p === 1 || "border-l",
                "text-link border-stone-400 px-2 py-1 underline dark:border-stone-500",
              ]}
              href={p === 1 ? "/stories" : `/stories/${p}`}
            >
              {p}
            </a>
          ),
        )
    }
  </div>
</GalleryLayout>
