'use client'

import { useMedusaProducts } from '@/hooks/useMedusaProducts'
import { useMedusaCart } from '@/hooks/useMedusaCart'
import Button from '@/components/ui/Button'
import Link from 'next/link'
import Image from 'next/image'

const CURRENCY_SYMBOLS: Record<string, string> = {
  zar: 'R', usd: '$', eur: '€', gbp: '£',
  kes: 'KSh', ngn: '₦', ghs: '₵'
}

const formatPrice = (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 } = useMedusaProducts(8)
  const { addToCart } = useMedusaCart()

  if (isLoading) {
    return (
      <div className="flex justify-center py-16">
        <div className="w-8 h-8 rounded-full border-2 border-[#18181b] border-t-transparent animate-spin" />
      </div>
    )
  }

  if (!products || products.length === 0) return null

  const handleAdd = (variantId: string) => {
    addToCart(variantId, 1)
    window.dispatchEvent(new CustomEvent('cart-drawer-open'))
  }

  return (
    <section
      id="featured-products"
      data-vula-section="featured-products"
      className="py-20 bg-white"
    >
      <div className="container mx-auto px-4">
        <h2 className="text-3xl md:text-4xl font-bold text-[#18181b] mb-2">
          Featured Collection
        </h2>
        <p className="text-gray-600 mb-10 max-w-xl">
          Our latest drops — quality tees built for the streets.
        </p>

        <div className="grid grid-cols-2 md:grid-cols-3 lg: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 rounded-xl border border-[#e5e7eb] bg-white shadow-[var(--shadow-card)] hover:shadow-[var(--shadow-lifted)] hover:-translate-y-0.5 transition-all duration-200 ease-out"
              >
                <div className="overflow-hidden rounded-t-xl">
                  <Link href={`/products/${product.id}`}>
                    <Image
                      src={product.thumbnail ?? '/placeholder-product.svg'}
                      alt={product.title}
                      width={400}
                      height={400}
                      className="object-cover w-full aspect-square group-hover:scale-105 transition-transform duration-500 ease-out"
                    />
                  </Link>
                </div>

                <div className="p-4 space-y-2">
                  <Link href={`/products/${product.id}`}>
                    <h3 className="font-semibold text-[#111827] truncate">
                      {product.title}
                    </h3>
                  </Link>

                  {price && (
                    <p className="text-[#36454F] font-bold text-sm">
                      {formatPrice(price.amount, price.currency_code)}
                    </p>
                  )}

                  {variant && (
                    <Button
                      variant="default"
                      size="sm"
                      className="w-full bg-[#18181b] hover:bg-[#36454F] text-white"
                      onClick={() => handleAdd(variant.id)}
                    >
                      Add to Cart
                    </Button>
                  )}
                </div>
              </div>
            )
          })}
        </div>
      </div>
    </section>
  )
}
