'use client'

import { useState } from 'react'
import Image from 'next/image'
import { X } from 'lucide-react'
import { useEmDashContent } from '@/hooks/useEmDashContent'

export default function GallerySection() {
  const { items, loading } = useEmDashContent('gallery')
  const [selected, setSelected] = useState<{ image: string; title: string; description: string } | null>(null)

  if (loading) return (
    <div className="flex justify-center py-16">
      <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-20 bg-white">
      <div className="container mx-auto px-4">
        <h2 className="text-[2rem] font-display font-bold text-[#18181b] text-center mb-4">Moments at vaVenda</h2>
        <p className="text-center text-[#211c0d] mb-12 max-w-2xl mx-auto font-serif italic">
          Every corner tells a story — from the warmth of our thatched lapa to the quiet of the bushveld at dawn.
        </p>
        <div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
          {items.map((item) => {
            const d = item.data as { title?: string; description?: string; image?: string }
            return (
              <div
                key={item.id}
                className="relative group cursor-pointer overflow-hidden rounded-xl aspect-square"
                onClick={() => setSelected({ image: d.image || '', title: d.title || '', description: d.description || '' })}
              >
                {d.image && (
                  <Image
                    src={d.image}
                    alt={d.title || 'Gallery image'}
                    fill
                    sizes="(max-width: 768px) 50vw, 33vw"
                    className="object-cover group-hover:scale-105 transition-transform duration-500"
                  />
                )}
                <div className="absolute inset-0 bg-black/0 group-hover:bg-black/30 transition-colors duration-300 flex items-end p-3">
                  <span className="text-white text-sm font-medium opacity-0 group-hover:opacity-100 transition-opacity duration-300">
                    {d.title || 'Untitled'}
                  </span>
                </div>
              </div>
            )
          })}
        </div>
        {/* Lightbox modal */}
        {selected && (
          <div
            className="fixed inset-0 z-50 bg-black/80 flex items-center justify-center p-4"
            onClick={() => setSelected(null)}
          >
            <div
              className="relative max-w-4xl w-full bg-white rounded-2xl overflow-hidden shadow-2xl"
              onClick={(e) => e.stopPropagation()}
            >
              <button
                className="absolute top-3 right-3 z-10 bg-white/80 rounded-full p-1 hover:bg-white transition-colors"
                onClick={() => setSelected(null)}
              >
                <X className="w-5 h-5 text-[#18181b]" />
              </button>
              <div className="relative h-[50vh] md:h-[60vh]">
                <Image
                  src={selected.image}
                  alt={selected.title}
                  fill
                  sizes="100vw"
                  className="object-contain"
                />
              </div>
              <div className="p-6 bg-white">
                <h3 className="text-xl font-semibold text-[#18181b]">{selected.title}</h3>
                <p className="mt-2 text-[#211c0d] font-serif italic">{selected.description}</p>
              </div>
            </div>
          </div>
        )}
      </div>
    </section>
  )
}
