import Link from "next/link";
import Image from "next/image";
import { Plus } from "lucide-react";
import { prisma } from "@/lib/prisma";
import { translationFor } from "@/lib/catalog";
import { AdminPageHeader } from "@/components/admin/page-header";
import { Button } from "@/components/ui/button";
import { toggleJournalPublishedAction } from "@/app/actions/admin-journal";

const dateFormatter = new Intl.DateTimeFormat("fr-FR", { day: "2-digit", month: "long", year: "numeric" });

export default async function AdminContentPage({
  searchParams,
}: {
  searchParams: Promise<Record<string, string | string[] | undefined>>;
}) {
  const query = await searchParams;
  const articles = await prisma.journalArticle.findMany({
    include: { translations: true },
    orderBy: { publishedAt: "desc" },
  });

  return (
    <div className="space-y-5">
      <AdminPageHeader
        eyebrow="Contenu"
        title="Journal"
        description="Les récits de la maison : gestes d'atelier, matières, lieux et inspirations."
        actions={
          <Button asChild size="sm">
            <Link href="/admin/content/new">
              <Plus className="h-4 w-4" />
              Nouvel article
            </Link>
          </Button>
        }
      />

      {typeof query.deleted === "string" && (
        <p className="border border-olive/25 bg-olive/5 px-4 py-3 text-[14px] text-olive">L'article a été supprimé.</p>
      )}

      <div className="border border-line bg-cream">
        {articles.map((article) => {
          const translation = translationFor(article.translations, "fr");
          return (
            <div key={article.id} className="flex flex-wrap items-center gap-4 border-b border-line px-4 py-3.5 last:border-0 hover:bg-cream/30">
              <div className="relative h-14 w-20 shrink-0 overflow-hidden bg-cream">
                <Image src={article.image} alt="" fill sizes="80px" className="object-cover" />
              </div>
              <div className="min-w-0 flex-1">
                <Link href={`/admin/content/${article.id}`} className="font-serif text-lg font-light text-charcoal hover:underline">
                  {translation?.title ?? article.slug}
                </Link>
                <p className="mt-0.5 text-[12px] text-pebble">
                  {article.author} · {dateFormatter.format(article.publishedAt)} · /{article.slug}
                </p>
              </div>
              <span className={article.isPublished ? "border border-olive/25 bg-olive/5 px-2 py-0.5 text-[12px] uppercase tracking-[0.1em] text-olive" : "border border-line bg-cream px-2 py-0.5 text-[12px] uppercase tracking-[0.1em] text-pebble"}>
                {article.isPublished ? "Publié" : "Brouillon"}
              </span>
              <div className="flex items-center gap-4">
                <form action={toggleJournalPublishedAction}>
                  <input type="hidden" name="id" value={article.id} />
                  <button type="submit" className="text-[12px] uppercase tracking-[0.12em] text-pebble hover:text-charcoal">
                    {article.isPublished ? "Dépublier" : "Publier"}
                  </button>
                </form>
                <Link href={`/admin/content/${article.id}`} className="text-[12px] uppercase tracking-[0.12em] text-pebble hover:text-charcoal">
                  Modifier
                </Link>
              </div>
            </div>
          );
        })}
        {articles.length === 0 && (
          <p className="px-4 py-16 text-center text-[14px] text-pebble">Aucun article. Écrivez le premier récit de la maison.</p>
        )}
      </div>
    </div>
  );
}
