// components/menu/MenuItem.tsx
'use client'

import { memo } from "react";
import Image from "next/image";
import PlusIcon from "@/components/icons/PlusIcon";
import MinusIcon from "@/components/icons/MinusIcon";
import { useReceiptStore } from "@/zustand/receiptStore";
import { motion } from "framer-motion";
interface MenuItemProps {
  food: {
    id: number
    name: string;
    contains: string;
    price: string;
  };
}

const MenuItem = ({ food }: MenuItemProps) => {
  const quantity = useReceiptStore(state => state.items[food.id]?.quantity || 0);

  const increment = useReceiptStore(state => state.increment);
  const decrement = useReceiptStore(state => state.decrement);

  return (
    <div className="flex gap-4 w-full h-full">
      <Image
        className="rounded-xl w-28 h-28"
        src={"/assets/images/food-preview.png"}
        height={100}
        width={100}
        alt="Food image"
      />
      <div className="w-full inline-flex flex-col justify-between">
        <div>
          <div className="text-sm2 font-normal text-black">{food.name}</div>
          <div className="text-[#7F7F7F] text-xs leading-5 font-normal mt-2">
            {food.contains}
          </div>
        </div>
        <div className="inline-flex justify-between w-full items-end">
          <span className="w-full" dir="ltr">{food.price} T</span>
          <motion.div
            whileTap={{ scale: 1.05 }}
            className="bg-background active:drop-shadow-xs max-w-[115px] rounded-md w-full h-8 inline-flex p-1 justify-between items-center gap-2"
          >
            {quantity <= 0 ? (
              <button
                onClick={() => increment(food.id)}
                className="inline-flex w-full justify-center items-center gap-2"
              >
                <PlusIcon />
                <div className="text-sm2 pt-0.5 font-normal text-black">افزودن</div>
              </button>
            ) : (
              <>
                <button
                  onClick={() => increment(food.id)}
                  className="bg-white hover:bg-white/60 active:bg-white/30 rounded-sm p-2"
                >
                  <PlusIcon />
                </button>
                <motion.div
                  key={quantity}
                  initial={{ scale: 1 }}
                  animate={{ scale: [1.2, 0.95, 1] }}
                  transition={{ duration: 0.3 }}
                  className="text-sm2 pt-0.5 font-semibold"
                >
                  {quantity}
                </motion.div>


                <button
                  onClick={() => decrement(food.id)}
                  className="bg-white hover:bg-white/60 active:bg-white/30 rounded-sm p-2"
                >
                  <MinusIcon />
                </button>
              </>
            )}
          </motion.div>
        </div>
      </div>
    </div>
  );
};

export default memo(MenuItem);
