"use client";

import { motion } from "framer-motion";
import Image from "next/image";
import Link from "next/link";
import type { Product } from "@/types/saleor";
import { getProductPrice, getProductImage, getLocalizedProduct } from "@/lib/saleor";

interface ProductCardProps {
  product: Product;
  index?: number;
  locale?: string;
}

export default function ProductCard({ product, index = 0, locale = "SR" }: ProductCardProps) {
  const image = getProductImage(product);
  const price = getProductPrice(product);
  const localized = getLocalizedProduct(product, locale);
  const isAvailable = product.variants?.[0]?.quantityAvailable > 0;

  return (
    <motion.div
      initial={{ opacity: 0, y: 20 }}
      whileInView={{ opacity: 1, y: 0 }}
      viewport={{ once: true }}
      transition={{ duration: 0.5, delay: index * 0.1 }}
    >
      <Link href={`/products/${localized.slug}`} className="group block">
        <div className="relative aspect-[4/5] bg-background-ice overflow-hidden mb-4">
          {image && (
            <Image
              src={image}
              alt={localized.name}
              fill
              className="object-cover transition-transform duration-500 group-hover:scale-105"
            />
          )}
          {!isAvailable && (
            <div className="absolute inset-0 bg-black/50 flex items-center justify-center">
              <span className="text-white font-medium">
                {locale === "en" ? "Out of Stock" : "Nema na stanju"}
              </span>
            </div>
          )}
        </div>
        
        <h3 className="font-serif text-lg mb-1 group-hover:text-accent-dark transition-colors">
          {localized.name}
        </h3>
        
        <p className="text-foreground-muted">
          {price || (locale === "en" ? "Contact for price" : "Kontaktirajte za cenu")}
        </p>
      </Link>
    </motion.div>
  );
}
