'use client'
import { useMedusaProducts } from '@/hooks/useMedusaProducts'
import { useRouter } from 'next/navigation'
import Button from '@/components/ui/Button'
import { ShoppingBag } from 'lucide-react'

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 FeaturedTeesSection() {
  const { products, isLoading } = useMedusaProducts(8)
  const router = useRouter()

  if (isLoading) {
    return (
      <div className="flex justify-center py-16 bg-[#ffffff]">
        <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

  return (
    <section
      id="featured-tees"
      data-vula-section="featured-tees"
      className="py-20 bg-[#ffffff]"
    >
      <div className="max-w-7xl mx-auto px-4">
        <div className="flex justify-between items-center mb-12">
          <h2 className="text-3xl md:text-4xl font-bold text-[#18181b] uppercase tracking-tight">
            Featured Collection
          </h2>
          <Button
            variant="outline"
            size="md"
            className="border-[#18181b] text-[#18181b] hover:bg-[#18181b] hover:text-white transition-all duration-200"
            onClick={() => router.push('/products')}
          >
            View All
          </Button>
        </div>

        <div className="grid grid-cols-1 sm:grid-cols-2 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 cursor-pointer bg-white rounded-xl border border-gray-200 shadow-[var(--shadow-card)] hover:shadow-[var(--shadow-lifted)] hover:-translate-y-0.5 transition-all duration-200 ease-out overflow-hidden"
              >
                <div className="aspect-square overflow-hidden bg-gray-50">
                  {product.thumbnail ? (
                    <img
                      src={product.thumbnail}
                      alt={product.title}
                      className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-500 ease-out"
                    />
                  ) : (
                    <div className="flex items-center justify-center h-full text-gray-300">
                      <ShoppingBag className="w-12 h-12" />
                    </div>
                  )}
                </div>
                <div className="p-4">
                  <h3 className="font-semibold text-[#111827] text-lg leading-tight">
                    {product.title}
                  </h3>
                  {price && (
                    <p className="mt-1 text-[#dc2626] font-bold">
                      {formatPrice(price.amount, price.currency_code)}
                    </p>
                  )}
                  <div className="mt-3 h-0.5 w-0 group-hover:w-full bg-[#dc2626] transition-all duration-300 ease-out" />
                </div>
              </div>
            )
          })}
        </div>

        <div className="mt-12 text-center">
          <Button
            variant="default"
            size="lg"
            className="bg-[#18181b] hover:bg-[#27272a] text-white px-10 py-4 rounded-lg font-semibold shadow-[var(--shadow-lifted)] transition-all duration-200 ease-out"
            onClick={() => router.push('/products')}
          >
            Shop All Tees
          </Button>
        </div>
      </div>
    </section>
  )
}