'use client'

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

export default function GuestRavesSection() {
  const { items, loading } = useEmDashContent('posts')

  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

  const testimonials = items.slice(0, 3)
  if (testimonials.length === 0) return null

  return (
    <section id="guest-reviews" data-vula-section="guestraves" data-vula-cms="posts" className="bg-[#f9f4f4] py-16">
      <div className="max-w-5xl mx-auto px-4">
        <h2 className="text-3xl md:text-4xl font-bold text-[#210d0d] mb-3 text-center tracking-tight">What our guests say</h2>
        <p className="text-[#210d0d] opacity-70 text-center mb-10 max-w-xl mx-auto">Real stories from travellers who found more than a room—they found a home in the Mara.</p>
        <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
          {testimonials.map((item) => {
            const d = item.data as { title?: string; excerpt?: string; author?: string; image?: string }
            return (
              <div key={item.id} className="bg-white rounded-xl p-6 border border-[#e2d4d4] shadow-[0_4px_20px_rgba(210,13,13,0.05)] flex flex-col">
                <span className="text-[#d4af37] text-lg mb-3">★★★★★</span>
                <p className="text-[#210d0d] opacity-80 italic text-sm leading-relaxed flex-1">"{d.excerpt}"</p>
                <div className="mt-4 pt-4 border-t border-[#e2d4d4] flex items-center gap-3">
                  {d.image && (
                    <div className="relative w-10 h-10 rounded-full overflow-hidden shrink-0">
                      <Image src={d.image} alt={d.author || 'Guest'} fill sizes="40px" className="object-cover" />
                    </div>
                  )}
                  <div>
                    <p className="text-sm font-semibold text-[#210d0d]">{d.author || 'Guest'}</p>
                    {d.title && <p className="text-xs text-[#210d0d] opacity-60">{d.title}</p>}
                  </div>
                </div>
              </div>
            )
          })}
        </div>
      </div>
    </section>
  )
}
