import Link from "next/link";
import { Package, Star, ArrowRight, Heart, Store } from "lucide-react";
import type { MarketplaceProduct } from "@/lib/marketplace-data";
import { getMarketplaceDisplayPrice } from "@/lib/marketplace-product-pricing";

interface PopularProductsProps {
  products: MarketplaceProduct[];
}

export function PopularProducts({ products }: PopularProductsProps) {
  if (!products || products.length === 0) return null;

  return (
    <section className="py-12 bg-gray-50/50">
      <div className="container mx-auto px-4 lg:px-8">
        
        {/* Header */}
        <div className="flex flex-col sm:flex-row sm:items-end justify-between gap-4 mb-8">
          <div>
            <div className="flex items-center gap-2 mb-2">
              <Package className="w-6 h-6 text-violet-600" />
              <h2 className="text-2xl font-bold text-gray-900">Produtos populares</h2>
            </div>
            <p className="text-gray-500">Os itens mais buscados do momento</p>
          </div>
          <Link href="/produtos" className="flex items-center gap-2 text-violet-600 font-medium hover:text-violet-700 transition-colors">
            Ver todos os produtos
            <ArrowRight className="w-4 h-4" />
          </Link>
        </div>

        {/* Products Grid */}
        <div className="flex overflow-x-auto lg:grid lg:grid-cols-4 gap-6 pb-4 scrollbar-hide snap-x" style={{ scrollbarWidth: 'none' }}>
          {products.slice(0, 4).map((product) => {
            const primaryImage = product.product_images?.find(img => img.is_primary)?.image_url || product.image_url;
            const fallbackImage = 'https://images.unsplash.com/photo-1542291026-7eec264c27ff?q=80&w=600&auto=format&fit=crop';
            
            const displayPrice = getMarketplaceDisplayPrice(product);

            return (
              <div key={product.id} className="flex-shrink-0 w-[240px] lg:w-auto snap-start bg-white border border-gray-100 rounded-3xl overflow-hidden hover:shadow-lg transition-all group flex flex-col">
                {/* Image Area */}
                <div className="aspect-[4/3] bg-gray-100 relative overflow-hidden">
                  <img 
                    src={primaryImage || fallbackImage} 
                    alt={product.name} 
                    className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-500"
                  />
                  
                  {/* Category Badge */}
                  <div className="absolute bottom-3 left-3">
                    <span className="px-2.5 py-1 bg-violet-600/90 backdrop-blur-sm text-white text-[10px] font-bold rounded-full uppercase tracking-wider">
                      {product.category || 'Produto'}
                    </span>
                  </div>

                  {/* Favorite Button */}
                  <button className="absolute top-3 right-3 w-8 h-8 bg-white/90 backdrop-blur-sm rounded-full flex items-center justify-center text-gray-400 hover:text-red-500 transition-colors shadow-sm">
                    <Heart className="w-4 h-4" />
                  </button>
                </div>
                
                {/* Content Area */}
                <div className="p-4 flex flex-col flex-1">
                  <Link href={`/produtos/${product.id}`} className="block group-hover:text-violet-600 transition-colors">
                    <h3 className="font-bold text-gray-900 line-clamp-2 leading-snug mb-2">
                      {product.name}
                    </h3>
                  </Link>
                  
                  <div className="mb-4">
                    {displayPrice.startsAt ? (
                      <span className="text-xs text-gray-500">A partir de </span>
                    ) : null}
                    <span className="font-bold text-gray-900 text-lg">
                      {displayPrice.label}
                    </span>
                  </div>
                  
                  <div className="mt-auto pt-4 border-t border-gray-100 flex items-center justify-between">
                    <Link href={`/lojas/${product.store?.slug}`} className="flex items-center gap-2 max-w-[70%] hover:opacity-80 transition-opacity">
                      <div className="w-6 h-6 rounded-full bg-gray-100 overflow-hidden border border-gray-200 flex-shrink-0">
                        {product.store?.logoUrl ? (
                          <img src={product.store.logoUrl} alt="" className="w-full h-full object-cover" />
                        ) : (
                          <Store className="w-3 h-3 m-1.5 text-gray-400" />
                        )}
                      </div>
                      <span className="text-xs font-medium text-gray-600 truncate">
                        {product.store?.name || "Loja Parceira"}
                      </span>
                    </Link>
                    
                    <div className="flex items-center gap-1 text-xs font-medium text-gray-700">
                      <Star className="w-3 h-3 fill-amber-400 text-amber-400" />
                      4.8
                    </div>
                  </div>
                </div>
              </div>
            );
          })}
        </div>

      </div>
    </section>
  );
}
