"use client";

import Image from "next/image";
import { useState } from "react";
import { Check, Plus, Star } from "lucide-react";

import { useLanguage } from "@/lib/i18n";
import { formatPrice, useCart } from "@/lib/cart";

type Product = {
  id: string;
  name: string;
  description: string | null;
  price: number;
  image: string | null;
  category: {
    id: string;
    name: string;
  };
  isAvailable: boolean;
  isFeatured: boolean;
};

interface MenuItemCardProps {
  item: Product;
}

export function MenuItemCard({
  item,
}: MenuItemCardProps) {
  const { t } = useLanguage();
  const { add } = useCart();

  const [added, setAdded] = useState(false);

  function handleAdd() {
    if (!item.isAvailable) {
      return;
    }

    add({
      id: item.id,
      name: item.name,
      description: item.description,
      price: Number(item.price),
      image: item.image,
      category: item.category.name,
      isAvailable: item.isAvailable,
      isFeatured: item.isFeatured,
    });

    setAdded(true);

    window.setTimeout(() => {
      setAdded(false);
    }, 1200);
  }

  const imageSrc =
    item.image?.trim() || "/placeholder.svg";

  return (
    <article className="group flex h-full flex-col overflow-hidden rounded-3xl border border-[#e8e1d6] bg-white shadow-sm transition-all duration-300 hover:-translate-y-1 hover:shadow-xl">
      <div className="relative aspect-[4/3] overflow-hidden bg-[#f5efe5]">
        <Image
          src={imageSrc}
          alt={item.name}
          fill
          sizes="(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 33vw"
          className="object-cover transition-transform duration-700 group-hover:scale-105"
        />

        <div className="pointer-events-none absolute inset-0 bg-gradient-to-t from-black/40 via-transparent to-transparent" />

        {item.isFeatured && (
          <div className="absolute left-4 top-4">
            <span className="inline-flex items-center gap-1.5 rounded-full bg-[#b8863b] px-3 py-1.5 text-xs font-bold text-white shadow-lg">
              <Star className="h-3.5 w-3.5 fill-current" />
              À la une
            </span>
          </div>
        )}

        <div className="absolute bottom-4 left-4">
          <span className="rounded-full bg-white/95 px-3 py-1.5 text-xs font-semibold text-[#6f5b3d] shadow-sm">
            {item.category.name}
          </span>
        </div>

        {!item.isAvailable && (
          <div className="absolute inset-0 flex items-center justify-center bg-black/50 backdrop-blur-[2px]">
            <span className="rounded-full border border-white/30 bg-white/95 px-5 py-2 text-sm font-bold text-gray-700 shadow-lg">
              {t("menu.unavailable")}
            </span>
          </div>
        )}
      </div>

      <div className="flex flex-1 flex-col p-5">
        <div className="flex items-start justify-between gap-4">
          <h3 className="font-serif text-xl font-semibold leading-tight text-[#241f1a]">
            {item.name}
          </h3>

          <div className="shrink-0 text-right">
            <span className="block whitespace-nowrap text-lg font-bold text-[#b8863b]">
              {formatPrice(Number(item.price))}
            </span>

            <span className="text-[11px] font-medium uppercase tracking-wide text-gray-400">
              {t("common.currency")}
            </span>
          </div>
        </div>

        <p className="mt-3 flex-1 text-sm leading-6 text-gray-500">
          {item.description ||
            "Une délicieuse sélection préparée avec soin par notre équipe."}
        </p>

        <div className="my-5 border-t border-[#eee8df]" />

        <button
          type="button"
          onClick={handleAdd}
          disabled={!item.isAvailable}
          className={`inline-flex w-full items-center justify-center gap-2 rounded-full px-5 py-3 text-sm font-semibold transition-all ${
            !item.isAvailable
              ? "cursor-not-allowed bg-gray-100 text-gray-400"
              : added
                ? "bg-green-600 text-white"
                : "bg-[#b8863b] text-white hover:bg-[#9f7332] hover:shadow-md"
          }`}
        >
          {added ? (
            <>
              <Check className="h-4 w-4" />
              Ajouté au panier
            </>
          ) : (
            <>
              <Plus className="h-4 w-4" />
              {t("menu.add")}
            </>
          )}
        </button>
      </div>
    </article>
  );
}