'use client'

import { useState } from 'react'
import { Star, ChevronLeft, ChevronRight, Quote } from 'lucide-react'
import { testimonials } from '@/lib/data'

export function Testimonials() {
  const [active, setActive] = useState(0)
  const go = (dir: number) =>
    setActive((a) => (a + dir + testimonials.length) % testimonials.length)

  return (
    <section id="temoignages" className="border-b border-border/60 py-16 lg:py-24">
      <div className="mx-auto max-w-5xl px-4 lg:px-8">
        <h2 className="text-balance text-center text-3xl font-bold tracking-tight sm:text-4xl">
          Ils ont construit leur plateforme avec Azelior
        </h2>

        {/* Desktop grid */}
        <div className="mt-12 hidden gap-5 md:grid md:grid-cols-3">
          {testimonials.map((t) => (
            <TestimonialCard key={t.name} t={t} />
          ))}
        </div>

        {/* Mobile carousel */}
        <div className="mt-12 md:hidden">
          <TestimonialCard t={testimonials[active]} />
          <div className="mt-6 flex items-center justify-center gap-4">
            <button
              type="button"
              onClick={() => go(-1)}
              className="rounded-full border border-border p-2"
              aria-label="Témoignage précédent"
            >
              <ChevronLeft className="size-4" />
            </button>
            <div className="flex gap-1.5">
              {testimonials.map((_, i) => (
                <span
                  key={i}
                  className={`size-2 rounded-full ${i === active ? 'bg-primary' : 'bg-border'}`}
                />
              ))}
            </div>
            <button
              type="button"
              onClick={() => go(1)}
              className="rounded-full border border-border p-2"
              aria-label="Témoignage suivant"
            >
              <ChevronRight className="size-4" />
            </button>
          </div>
        </div>
      </div>
    </section>
  )
}

function TestimonialCard({
  t,
}: {
  t: { quote: string; name: string; role: string; rating: number }
}) {
  return (
    <figure className="flex h-full flex-col rounded-2xl border border-border bg-card p-6">
      <Quote className="size-6 text-primary/60" />
      <blockquote className="mt-4 flex-1 text-sm leading-relaxed text-muted-foreground">
        “{t.quote}”
      </blockquote>
      <div className="mt-5 flex items-center gap-0.5 text-primary">
        {Array.from({ length: t.rating }).map((_, i) => (
          <Star key={i} className="size-4 fill-current" />
        ))}
      </div>
      <figcaption className="mt-2">
        <div className="text-sm font-semibold">{t.name}</div>
        <div className="text-xs text-muted-foreground">{t.role}</div>
      </figcaption>
    </figure>
  )
}
