"use client"

import Image from "next/image"
import type { CartItem as CartItemType } from "@/types/cart"
import { formatPrice } from "@/lib/formatters"
import { useCart } from "@/hooks/useCart"

type CartItemProps = {
  item: CartItemType
}

export function CartItem({ item }: CartItemProps) {
  const { updateQuantity, removeItem } = useCart()
  const { product, quantity, size, color } = item

  return (
    <div className="flex gap-4 py-4">
      <div className="relative h-24 w-24 flex-shrink-0 overflow-hidden rounded-lg bg-surface">
        <Image src={product.image} alt={product.name} fill className="object-cover" />
      </div>

      <div className="flex flex-1 flex-col">
        <div className="flex justify-between">
          <div>
            <h3 className="text-sm font-medium text-white">{product.name}</h3>
            <p className="mt-0.5 text-xs capitalize text-silver">
              {size} · {color}
            </p>
          </div>
          <p className="text-sm font-medium text-white">
            {formatPrice(product.price * quantity)}
          </p>
        </div>

        <div className="mt-auto flex items-center justify-between">
          <div className="flex items-center gap-2">
            <button
              onClick={() => updateQuantity(product.id, size, color, quantity - 1)}
              className="flex h-6 w-6 items-center justify-center rounded border border-white/20 text-sm text-white transition-colors hover:border-gold hover:text-gold"
              aria-label="Decrease quantity"
            >
              −
            </button>
            <span className="w-6 text-center text-sm text-white">{quantity}</span>
            <button
              onClick={() => updateQuantity(product.id, size, color, quantity + 1)}
              className="flex h-6 w-6 items-center justify-center rounded border border-white/20 text-sm text-white transition-colors hover:border-gold hover:text-gold"
              aria-label="Increase quantity"
            >
              +
            </button>
          </div>
          <button
            onClick={() => removeItem(product.id, size, color)}
            className="text-xs text-silver underline transition-colors hover:text-white"
          >
            Remove
          </button>
        </div>
      </div>
    </div>
  )
}
