import type { Product } from "@/mocks/dshnCommerce";
import { businesses } from "@/mocks/dshn";
import { PriceText, cn } from "./ui";

export default function ProductCard({
  product,
  onClick,
  onClickBusiness,
}: {
  product: Product;
  onClick?: () => void;
  onClickBusiness?: () => void;
}) {
  const out = product.stock === 0;
  const business = businesses.find((b) => b.id === product.businessId);
  return (
    <div
      onClick={onClick}
      className={cn(
        "bg-background-100 border border-background-200 rounded-[20px] overflow-hidden cursor-pointer hover:border-background-300 transition-colors",
        out && "opacity-55",
      )}
    >
      <div className="relative w-full aspect-square bg-background-200">
        <img src={product.image} alt={product.name} className="w-full h-full object-cover object-top" />
        {out && (
          <span className="absolute top-2 right-2 rounded-full bg-foreground-950/85 text-background-50 text-[10px] font-semibold px-2 py-1">
            نفد المخزون
          </span>
        )}
      </div>
      <div className="p-3">
        <h3 className="text-[13px] font-semibold text-foreground-950 leading-snug line-clamp-2 h-9">
          {product.name}
        </h3>
        <div className="mt-2">
          <PriceText price={product.price} negotiable={product.negotiable} />
        </div>
        {business && (
          <button
            type="button"
            onClick={(e) => {
              e.stopPropagation();
              onClickBusiness?.();
            }}
            className="mt-2 flex items-center gap-1 text-[11px] font-medium text-accent-700 hover:text-accent-800 cursor-pointer max-w-full"
          >
            <i className="ri-store-2-line text-[12px] leading-none shrink-0" />
            <span className="truncate underline underline-offset-2">{business.name}</span>
          </button>
        )}
      </div>
    </div>
  );
}