'use client'
import { useFeaturedProducts } from '@/hooks/useFeaturedProducts'
import Link from 'next/link'
import { ShoppingBag } from 'lucide-react'
import { useMedusaCart } from '@/hooks/useMedusaCart'

const CURRENCY_SYMBOLS: Record<string, string> = { zar: 'R', usd: '$', eur: '€', gbp: '£', kes: 'KSh', ngn: '₦', ghs: '₵' }
const fmt = (amount: number, code?: string) => {
  const sym = CURRENCY_SYMBOLS[(code ?? '').toLowerCase()] ?? (code?.toUpperCase() ?? '')
  return `${sym}${(amount / 100).toFixed(2)}`
}

export default function FeaturedProductsSection() {
  const { products, isLoading } = useFeaturedProducts(8)
  const { addToCart } = useMedusaCart()

  if (isLoading) {
    return (
      <section className="py-20">
        <div className="container mx-auto px-4">
          <div className="grid grid-cols-2 md:grid-cols-4 gap-6">
            {Array.from({ length: 8 }).map((_, i) => (
              <div key={i} className="aspect-square rounded-xl bg-muted animate-pulse" />
            ))}
          </div>
        </div>
      </section>
    )
  }

  if (!products.length) return null

  return (
    <section data-vula-section="featuredproducts" className="py-20">
      <div className="container mx-auto px-4">
        <div className="flex items-center justify-between mb-10">
          <div>
            <p className="text-xs uppercase tracking-widest text-primary font-semibold mb-1">Curated for You</p>
            <h2 className="text-3xl font-bold text-foreground">Featured Products</h2>
          </div>
          <Link href="/products" className="text-sm font-semibold text-primary hover:underline underline-offset-4">View all →</Link>
        </div>
        <div className="grid grid-cols-2 md:grid-cols-4 gap-6">
          {products.map((product) => {
            const variant = product.variants?.[0]
            const price = variant?.prices?.[0]
            return (
              <div key={product.id} className="group bg-card rounded-xl overflow-hidden border border-border hover:shadow-md transition-all">
                <Link href={`/products/${product.id}`} className="block">
                  <div className="relative aspect-square bg-muted">
                    {product.thumbnail ? (
                      <img src={product.thumbnail} alt={product.title || ''} className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-500" />
                    ) : (
                      <div className="w-full h-full flex items-center justify-center">
                        <ShoppingBag className="h-8 w-8 text-muted-foreground/30" />
                      </div>
                    )}
                  </div>
                </Link>
                <div className="p-4">
                  <Link href={`/products/${product.id}`}>
                    <p className="text-sm font-semibold text-foreground line-clamp-2 mb-1 hover:text-primary transition-colors">{product.title}</p>
                  </Link>
                  {price && (
                    <p className="text-sm font-bold text-primary mb-3">{fmt(price.amount, price.currency_code)}</p>
                  )}
                  <button
                    onClick={() => { if (!variant?.id) return; addToCart(variant.id, 1); window.dispatchEvent(new CustomEvent('cart-drawer-open')) }}
                    disabled={!variant?.id}
                    className="w-full text-xs bg-primary/10 text-primary hover:bg-primary hover:text-primary-foreground py-2 rounded-lg transition-colors font-medium disabled:opacity-40"
                  >
                    Add to Cart
                  </button>
                </div>
              </div>
            )
          })}
        </div>
      </div>
    </section>
  )
}
