'use client'
import { useEmDashContent } from '@/hooks/useEmDashContent'
import Image from 'next/image'

export default function GallerySection() {
  const { items, loading } = useEmDashContent('gallery')

  if (loading) return <div className="flex justify-center py-24"><div className="w-8 h-8 rounded-full border-2 border-current border-t-transparent animate-spin" /></div>
  if (items.length === 0) return null

  return (
    <section id="gallery" data-vula-section="gallery" data-vula-cms="gallery" className="py-24 bg-[#1c1612]">
      <div className="max-w-7xl mx-auto px-4">
        <div className="flex items-center justify-center gap-4 mb-12">
          <span className="block w-14 h-px bg-[#d4af37]/50" />
          <span className="text-[#d4af37] tracking-[0.2em] text-xs font-sans uppercase">Visual Diary</span>
          <span className="block w-14 h-px bg-[#d4af37]/50" />
        </div>
        <h2 className="text-4xl md:text-5xl font-light text-[#eeeae7] text-center mb-16" style={{ fontFamily: 'Cormorant Garamond, serif' }}>
          <span className="italic text-[#d4af37]">Moments</span> at Masai Suites
        </h2>
        <div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
          {items.slice(0, 8).map((item) => {
            const d = item.data as { title?: string; description?: string; image?: string }
            return (
              <div key={item.id} className="group relative aspect-square overflow-hidden rounded-sm border border-[#403630]/50">
                {d.image ? (
                  <Image
                    src={d.image}
                    alt={d.title ?? 'Gallery image'}
                    fill
                    sizes="(max-width: 768px) 50vw, (max-width: 1024px) 33vw, 25vw"
                    className="object-cover transition-transform duration-500 group-hover:scale-105"
                  />
                ) : (
                  <div className="h-full w-full bg-[#2a1f1a]" />
                )}
                {d.title && (
                  <div className="absolute inset-0 bg-gradient-to-t from-black/70 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300 flex items-end p-3">
                    <span className="text-[#eeeae7] text-sm font-sans tracking-wide">{d.title}</span>
                  </div>
                )}
              </div>
            )
          })}
        </div>
      </div>
    </section>
  )
}
