import { getTranslations } from 'next-intl/server'
import { Quote, Star } from 'lucide-react'
import { getTestimonials } from '@/lib/db/queries/getTestimonials'
import { getStoreStats } from '@/lib/db/queries/getStoreStats'

function Stars({ rating, size = 14 }: { rating: number; size?: number }) {
  return (
    <span className="flex gap-0.5" aria-label={`${rating}/5`}>
      {Array.from({ length: 5 }, (_, i) => (
        <Star key={i} size={size} className={i < Math.round(rating) ? 'fill-yellow-400 text-yellow-400' : 'text-gray-300'} />
      ))}
    </span>
  )
}

// "Premium": the store's real average rating (approved reviews) as a big
// score card, beside the admin-curated testimonials.
export default async function ReviewsPremium() {
  const [t, tTestimonials, testimonials, stats] = await Promise.all([
    getTranslations('HomeDesigns'),
    getTranslations('Testimonials'),
    getTestimonials(),
    getStoreStats(),
  ])
  if (testimonials.length === 0) return null

  return (
    <section className="home-reveal grid gap-4 sm:gap-6 lg:grid-cols-12">
      <div className="relative flex flex-col justify-between overflow-hidden rounded-[2rem] bg-dark-bg p-8 text-white lg:col-span-4">
        <div aria-hidden className="absolute -bottom-20 -start-16 h-64 w-64 rounded-full bg-primary/30 blur-[90px]" />
        <div className="relative">
          <span className="text-[11px] font-bold uppercase tracking-[0.3em] text-primary-light">{t('testimonials.eyebrow')}</span>
          <h2 className="mt-3 text-3xl sm:text-4xl font-black tracking-tight leading-tight">{tTestimonials('title')}</h2>
        </div>
        {stats.reviews > 0 && (
          <div className="relative mt-10">
            <p className="text-7xl font-black tracking-tighter">{stats.averageRating.toFixed(1)}</p>
            <div className="mt-2 flex items-center gap-3">
              <Stars rating={stats.averageRating} size={18} />
              <span className="text-sm text-white/65">{t('premium.basedOn', { count: stats.reviews })}</span>
            </div>
          </div>
        )}
      </div>

      <div className="grid gap-4 sm:gap-6 sm:grid-cols-2 lg:col-span-8">
        {testimonials.slice(0, 4).map((item) => (
          <figure key={item.id} className="flex flex-col justify-between gap-6 rounded-[2rem] bg-white p-6 sm:p-7 ring-1 ring-gray-100 shadow-sm">
            <div>
              <Quote size={28} className="text-primary/25" />
              <blockquote className="mt-3 text-base sm:text-lg leading-relaxed text-dark text-pretty">{item.text}</blockquote>
            </div>
            <figcaption className="flex items-center justify-between gap-3 border-t border-gray-100 pt-4">
              <span className="flex items-center gap-3">
                <span className="flex h-10 w-10 items-center justify-center rounded-full bg-primary/10 font-black text-primary">
                  {item.name.charAt(0).toUpperCase()}
                </span>
                <span className="font-semibold text-dark">{item.name}</span>
              </span>
              <Stars rating={item.rating} />
            </figcaption>
          </figure>
        ))}
      </div>
    </section>
  )
}
