'use client'

import { useEmDashContent } from '@/hooks/useEmDashContent'
import Image from 'next/image'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/Card'
import Button from '@/components/ui/Button'

export default function CurrentCampaignsSection() {
  const { items, loading } = useEmDashContent('campaigns')

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

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

  return (
    <section
      id="current-campaigns"
      data-vula-section="currentcampaigns"
      data-vula-cms="campaigns"
      className="py-20 bg-neutral-50"
    >
      <div className="max-w-7xl mx-auto px-4">
        <div className="text-center mb-12">
          <h2 className="text-3xl font-bold text-[#000080]">Community Campaigns</h2>
          <p className="text-[#36454F] mt-2 max-w-2xl mx-auto">
            Justice doesn\'t end in the courtroom. See how we\'re giving back.
          </p>
        </div>

        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
          {items.map((item) => {
            const d = item.data as {
              title?: string
              headline?: string
              body?: string
              cta_text?: string
              image?: string
            }
            return (
              <Card
                key={item.id}
                className="rounded-xl border border-neutral-100 bg-white shadow-[var(--shadow-card)] hover:shadow-[var(--shadow-lifted)] hover:-translate-y-0.5 transition-all duration-200 ease-out overflow-hidden"
              >
                {d.image && (
                  <div className="aspect-video overflow-hidden">
                    <Image
                      src={d.image}
                      alt={d.title || 'Campaign image'}
                      width={800}
                      height={600}
                      className="object-cover w-full h-full hover:scale-105 transition-transform duration-500 ease-out"
                    />
                  </div>
                )}
                <CardHeader>
                  {d.title && <CardTitle className="text-lg text-[#000080]">{d.title}</CardTitle>}
                  {d.headline && (
                    <p className="text-[#d4af37] text-sm font-semibold">{d.headline}</p>
                  )}
                </CardHeader>
                <CardContent>
                  {d.body && (
                    <p className="text-[#36454F] text-sm mb-4 line-clamp-3">{d.body}</p>
                  )}
                  {d.cta_text && (
                    <Button variant="default" size="sm">
                      {d.cta_text}
                    </Button>
                  )}
                </CardContent>
              </Card>
            )
          })}
        </div>
      </div>
    </section>
  )
}