'use client'

import { useVulaContent } from '@/hooks/useVulaContent'
import Image from 'next/image'
import Link from 'next/link'
import Button from '@/components/ui/Button'

export default function IkhambiBlogSection() {
  const { items, loading } = useVulaContent('posts')

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

  return (
    <section id="blog" data-vula-section="ikhambiblog" data-vula-cms="posts" className="py-20 bg-[#f4f5f2]">
      <div className="max-w-6xl mx-auto px-4">
        <div className="text-center mb-12">
          <h2 className="text-3xl md:text-4xl font-bold text-[#2c331f]">
            <span className="text-[#8b5a3c]">Ikhambi</span> — wisdom from the earth
          </h2>
          <p className="text-[#6b8e23] mt-2">Deepen your ritual with stories and guides.</p>
        </div>
        <div className="grid md:grid-cols-3 gap-8">
          {items.slice(0, 3).map((item) => {
            const d = item.data as { title?: string; excerpt?: string; author?: string; image?: string; category?: string }
            return (
              <div key={item.id} className="bg-white rounded-[28px] overflow-hidden shadow-lg hover:shadow-xl transition-all duration-200 group">
                {d.image && (
                  <div className="relative h-48 overflow-hidden">
                    <Image src={d.image} alt={d.title || 'Blog image'} fill className="object-cover group-hover:scale-105 transition-transform duration-500" sizes="(max-width: 768px) 100vw, 33vw" />
                  </div>
                )}
                <div className="p-6">
                  {d.category && <span className="text-xs font-semibold text-[#ea580c] uppercase tracking-wider">{d.category}</span>}
                  {d.title && <h3 className="text-xl font-bold text-[#8b5a3c] mt-2 mb-2 leading-snug">{d.title}</h3>}
                  {d.excerpt && <p className="text-[#2c331f]/80 text-sm mb-4 line-clamp-3">{d.excerpt}</p>}
                  {d.author && <p className="text-xs text-[#6b8e23] mb-4">By {d.author}</p>}
                  <Link href="/blog">
                    <Button variant="ghost" size="sm" className="text-[#6b8e23] hover:text-[#6b8e23]/80 p-0">
                      Read more →
                    </Button>
                  </Link>
                </div>
              </div>
            )
          })}
        </div>
      </div>
    </section>
  )
}
