import { Handlers, PageProps } from "$fresh/server.ts";
import { MainLayout } from "@components/layouts/main.tsx";
import IconArrowLeft from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/arrow-left.tsx";
import { Article, getAllArticles } from "@lib/resource/articles.ts";
import { Card } from "@components/Card.tsx";
import { KMenu } from "@islands/KMenu.tsx";

export const handler: Handlers<Article[] | null> = {
  async GET(_, ctx) {
    const movies = await getAllArticles();
    return ctx.render(movies);
  },
};

export default function Greet(props: PageProps<Article[] | null>) {
  return (
    <MainLayout url={props.url}>
      <header class="flex gap-4 items-center mb-5 md:hidden">
        <a
          class="px-4 ml-4 py-2 bg-gray-300 text-gray-800 rounded-lg flex items-center gap-1"
          href="/"
        >
          <IconArrowLeft class="w-5 h-5" />
          Back
        </a>

        <h3 class="text-2xl text-white font-light">📝 Articles</h3>
      </header>

      <KMenu type="main" context={false} />
      <div class="flex flex-wrap items-center gap-4 px-4">
        {props.data?.map((doc) => {
          return <Card link={`/articles/${doc.id}`} title={doc.name} />;
        })}
      </div>
    </MainLayout>
  );
}
