"use client";

import Image from "next/image";
import Link from "next/link";
import { useSyncExternalStore } from "react";
import type { ShopProduct } from "@/data/shop";
import { shopAssets } from "@/data/shop";
import { isProductInCart, subscribeCartChanges } from "@/lib/cart-add";

const iconImgStyle = { width: "auto", height: "auto" } as const;
const STAR_SIZE = 16;

type ShopProductCardProps = {
  product: ShopProduct;
  onAddToCart?: () => void;
  /** Grid column classes; default matches main shop grid. */
  columnClassName?: string;
};

const DEFAULT_COLUMNS = "col-xl-4 col-lg-6 col-md-6";

export default function ShopProductCard({
  product,
  onAddToCart,
  columnClassName = DEFAULT_COLUMNS,
}: ShopProductCardProps) {
  const isAddedToCart = useSyncExternalStore(
    subscribeCartChanges,
    () => isProductInCart(product.id),
    () => false,
  );
  const stars = Array.from({ length: product.rating }, (_, i) => i);
  const productHref =
    product.href && product.href !== "#"
      ? product.href
      : `/product-details/${product.id}`;

  return (
    <div className={columnClassName}>
      <div
        className="shop-card"
        data-aos="fade-up"
        data-aos-duration={900}
        data-aos-delay={product.aosDelay}
      >
        <div className="shop-img">
          <Link href={productHref}>
            <Image
              src={product.imageSrc}
              alt={product.imageAlt}
              width={product.imageWidth}
              height={product.imageHeight}
              sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
              className="w-100 h-auto"
            />
          </Link>
          {product.onSale ? <span className="sale">Sale!</span> : null}
          {isAddedToCart ? (
            <Link href="/cart" className="btn-style1">
              View Cart
              <span>
                <Image
                  src={shopAssets.arrowIcon}
                  alt={shopAssets.arrowAlt}
                  width={14}
                  height={14}
                />
              </span>
            </Link>
          ) : (
            <a
              href="#"
              className="btn-style1"
              onClick={(e) => {
                e.preventDefault();
                onAddToCart?.();
              }}
            >
              Add to Cart
              <span>
                <Image
                  src={shopAssets.arrowIcon}
                  alt={shopAssets.arrowAlt}
                  width={14}
                  height={14}
                />
              </span>
            </a>
          )}
        </div>
        <div className="shop-content">
          <h3 className="title">
            <Link href={productHref}>{product.title}</Link>
          </h3>
          <ul className="rating-area">
            {stars.map((i) => (
              <li key={i}>
                <Image
                  src={shopAssets.ratingIcon}
                  alt={shopAssets.ratingAlt}
                  width={STAR_SIZE}
                  height={STAR_SIZE}
                  style={iconImgStyle}
                />
              </li>
            ))}
          </ul>
          <span className="price2">
            {product.compareAtPrice ? (
              <del>{product.compareAtPrice}</del>
            ) : null}
            <span>{product.price}</span>
          </span>
        </div>
      </div>
    </div>
  );
}
