import Link from "next/link";
import Image from "next/image";
import { Pencil, Plus } from "lucide-react";
import { prisma } from "@/lib/prisma";
import { categoryDescription, categoryTitle } from "@/lib/catalog";
import { AdminPageHeader } from "@/components/admin/page-header";
import { Button } from "@/components/ui/button";
import { toggleCategoryActiveAction } from "@/app/actions/admin-categories";

export default async function AdminCategoriesPage({
  searchParams,
}: {
  searchParams: Promise<Record<string, string | string[] | undefined>>;
}) {
  const query = await searchParams;
  const categories = await prisma.category.findMany({
    include: {
      translations: true,
      _count: { select: { products: true } },
    },
    orderBy: { position: "asc" },
  });

  return (
    <div className="space-y-5">
      <AdminPageHeader
        eyebrow="Catalogue"
        title="Catégories"
        description="Les familles qui structurent la boutique, dans l'ordre où elles apparaissent en navigation."
        actions={
          <Button asChild size="sm">
            <Link href="/admin/categories/new">
              <Plus className="h-4 w-4" />
              Nouvelle catégorie
            </Link>
          </Button>
        }
      />

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

      <div className="grid gap-3 md:grid-cols-2 xl:grid-cols-3">
        {categories.map((category) => (
          <div key={category.id} className="group border border-line bg-cream">
            <div className="relative aspect-[16/9] overflow-hidden bg-cream">
              <Image src={category.image} alt={categoryTitle(category, "fr")} fill sizes="(max-width: 768px) 100vw, 33vw" className="object-cover transition-transform duration-700 group-hover:scale-[1.02]" />
              <div className="absolute left-2 top-2 flex gap-1">
                <span className={category.isActive ? "bg-olive/90 px-2 py-0.5 text-[12px] uppercase tracking-[0.1em] text-ivory" : "bg-charcoal/70 px-2 py-0.5 text-[12px] uppercase tracking-[0.1em] text-ivory"}>
                  {category.isActive ? "Active" : "Inactive"}
                </span>
              </div>
            </div>
            <div className="p-4">
              <div className="flex items-start justify-between gap-2">
                <div>
                  <h2 className="font-serif text-xl font-light">{categoryTitle(category, "fr")}</h2>
                  <p className="mt-0.5 font-mono text-[12px] text-pebble">/{category.slug}</p>
                </div>
                <span className="border border-line px-2 py-0.5 text-[12px] text-pebble">
                  {category._count.products} produit(s)
                </span>
              </div>
              <p className="mt-2 line-clamp-2 text-[13px] leading-relaxed text-pebble">
                {categoryDescription(category, "fr")}
              </p>
              <div className="mt-4 flex items-center justify-between border-t border-line pt-3">
                <form action={toggleCategoryActiveAction}>
                  <input type="hidden" name="id" value={category.id} />
                  <button type="submit" className="text-[12px] uppercase tracking-[0.12em] text-pebble hover:text-charcoal">
                    {category.isActive ? "Désactiver" : "Activer"}
                  </button>
                </form>
                <Link
                  href={`/admin/categories/${category.id}`}
                  className="flex h-7 items-center gap-1.5 border border-line px-2 text-[12px] uppercase tracking-[0.1em] text-pebble hover:border-charcoal/40 hover:text-charcoal"
                >
                  <Pencil className="h-3 w-3" />
                  Modifier
                </Link>
              </div>
            </div>
          </div>
        ))}
        {categories.length === 0 && (
          <p className="border border-dashed border-line px-6 py-16 text-center text-[14px] text-pebble md:col-span-2 xl:col-span-3">
            Aucune catégorie pour le moment.
          </p>
        )}
      </div>
    </div>
  );
}
