'use client'

import { useEmDashContent } from "@/hooks/useEmDashContent"
import Image from "next/image"
import { useState } from "react"

export default function VendaVisionsGallerySection() {
  const { items, loading } = useEmDashContent("gallery")
  const [selectedIndex, setSelectedIndex] = useState<number | null>(null)

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

  if (items.length === 0) return null

  return (
    <section
      id="gallery"
      data-vula-section="vendavisionsgallery"
      data-vula-cms="gallery"
      className="py-20 bg-white"
    >
      <div className="container mx-auto px-4 max-w-7xl">
        <h2 className="text-4xl md:text-5xl font-heading text-[#0d2114] mb-4">
          Visual stories from the lodge
        </h2>
        <p className="text-lg text-[#8b5a3c] mb-12 max-w-2xl">
          A gallery that moves from architecture to landscape to craft — every image a glimpse into life at vaVenda.
        </p>
        <div className="grid grid-cols-2 md:grid-cols-3 gap-4">
          {items.map((item, idx) => {
            const d = item.data as { title?: string; description?: string; image?: string }
            return (
              <div
                key={item.id}
                className="relative aspect-square overflow-hidden rounded-xl cursor-pointer group"
                onClick={() => setSelectedIndex(idx)}
              >
                {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 ease-out"
                  />
                )}
                <div className="absolute inset-0 bg-black/0 group-hover:bg-black/20 transition-colors duration-300 flex items-end p-4">
                  <p className="text-white font-heading text-sm opacity-0 group-hover:opacity-100 transition-opacity duration-300">
                    {d.title}
                  </p>
                </div>
              </div>
            )
          })}
        </div>
        {selectedIndex !== null && (
          <div
            className="fixed inset-0 bg-black/80 z-50 flex items-center justify-center p-4"
            onClick={() => setSelectedIndex(null)}
          >
            <div className="relative max-w-4xl w-full aspect-video">
              {items[selectedIndex] && (
                <Image
                  src={(items[selectedIndex].data as { image?: string }).image || ""}
                  alt={(items[selectedIndex].data as { title?: string }).title || "Gallery"}
                  fill
                  sizes="100vw"
                  className="object-contain"
                />
              )}
            </div>
            <button
              className="absolute top-4 right-4 text-white text-2xl"
              onClick={() => setSelectedIndex(null)}
              aria-label="Close lightbox"
            >
              &times;
            </button>
          </div>
        )}
      </div>
    </section>
  )
}
