'use client'

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

export default function AdvocateProfilesSection() {
  const { items, loading } = useEmDashContent('team_members')

  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 || items.length === 0) return null

  return (
    <section id="attorneys" data-vula-section="advocateprofiles" data-vula-cms="team_members" className="py-20 bg-white">
      <div className="container mx-auto px-4">
        <div className="text-center mb-12">
          <h2 className="text-3xl md:text-4xl font-bold text-[#000080]">
            Meet Your Defence Team
          </h2>
          <p className="mt-4 text-lg text-gray-600 max-w-2xl mx-auto">
            Seasoned advocates and attorneys who bring decades of courtroom experience to every case.
          </p>
        </div>
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
          {items.map((item) => {
            const d = item.data as { name?: string; role?: string; bio?: string; image?: string }
            return (
              <Card key={item.id} className="group hover:shadow-[var(--shadow-lifted)] hover:-translate-y-0.5 transition-all duration-200 ease-out">
                <CardHeader className="p-0">
                  <div className="relative aspect-square overflow-hidden rounded-t-xl">
                    {d.image ? (
                      <Image
                        src={d.image}
                        alt={d.name ?? 'Team member'}
                        fill
                        className="object-cover group-hover:scale-105 transition-transform duration-500 ease-out"
                        sizes="(max-width: 768px) 100vw, 33vw"
                      />
                    ) : (
                      <div className="w-full h-full bg-[#36454F]/20 flex items-center justify-center">
                        <span className="text-4xl text-[#36454F]/40">{d.name?.charAt(0) ?? '?'}</span>
                      </div>
                    )}
                  </div>
                </CardHeader>
                <CardContent className="p-6">
                  <CardTitle className="text-xl font-semibold text-[#000080] mb-1">
                    {d.name ?? 'Advocate'}
                  </CardTitle>
                  {d.role && (
                    <span className="text-sm text-[#d4af37] font-medium">{d.role}</span>
                  )}
                  {d.bio && (
                    <p className="mt-3 text-sm text-gray-600 leading-relaxed line-clamp-3">
                      {d.bio}
                    </p>
                  )}
                </CardContent>
              </Card>
            )
          })}
        </div>
      </div>
    </section>
  )
}