import Image from 'next/image'
import Link from 'next/link'
import { Clock, Newspaper } from 'lucide-react'
import { getLocale, getTranslations } from 'next-intl/server'
import { getBlogList } from '@/lib/db/queries/getBlogPosts'
import SectionHeading from './SectionHeading'
import { isOptimizableImage } from '@/lib/cloudinary'

// Latest published blog posts (getBlogList — cached, POSTS_TAG). Hidden
// when the blog has no posts yet.
export default async function BlogTeaser({ variant }: { variant: 'modern' | 'premium' }) {
  const locale = await getLocale()
  const limit = 3
  const [t, { posts }] = await Promise.all([
    getTranslations('HomeDesigns'),
    getBlogList(locale, { page: 1, limit, categorySlug: null, search: null }),
  ])
  if (posts.length === 0) return null

  const dateFormat = new Intl.DateTimeFormat(locale, { day: 'numeric', month: 'short', year: 'numeric' })

  return (
    <section className="home-reveal">
      <SectionHeading
        variant={variant}
        eyebrow={t('blog.eyebrow')}
        title={t('blog.title')}
        action={{ href: '/blogs', label: t('blog.viewAll') }}
      />
      <div className="grid gap-6 sm:gap-8 md:grid-cols-3">
        {posts.map((post) => (
          <article key={post.id} className="group">
            <Link href={`/blog/${post.slug}`} className="block">
              <div
                className={`relative overflow-hidden bg-gray-100 ${
                  variant === 'premium' ? 'aspect-[4/3] rounded-[1.75rem]' : 'aspect-[16/10] rounded-2xl'
                }`}
              >
                {post.imageUrl ? (
                  <Image
                    src={post.imageUrl}
                    alt={post.title}
                    fill
                    sizes="(max-width: 768px) 100vw, 33vw"
                    unoptimized={!isOptimizableImage(post.imageUrl)}
                    className="object-cover text-transparent transition-transform duration-700 ease-out group-hover:scale-105"
                  />
                ) : (
                  <div aria-hidden className="absolute inset-0 flex items-center justify-center bg-brand-tint">
                    <Newspaper size={40} strokeWidth={1.2} className="text-primary/40" />
                  </div>
                )}
                {post.categoryName && variant === 'modern' && (
                  <span className="absolute top-3 start-3 rounded-full bg-white/90 px-3 py-1 text-xs font-semibold text-dark backdrop-blur">
                    {post.categoryName}
                  </span>
                )}
              </div>
              <div className="mt-4 flex items-center gap-3 text-xs text-gray-custom">
                <time dateTime={post.publishedAt}>{dateFormat.format(new Date(post.publishedAt))}</time>
                <span aria-hidden>•</span>
                <span className="inline-flex items-center gap-1">
                  <Clock size={12} />
                  {t('blog.minRead', { count: post.readTimeMinutes })}
                </span>
              </div>
              <h3
                className={`mt-2 text-dark text-balance transition-colors group-hover:text-primary ${
                  variant === 'premium' ? 'text-xl font-black tracking-tight leading-snug' : 'text-lg font-bold leading-snug'
                }`}
              >
                {post.title}
              </h3>
              {post.excerpt && <p className="mt-2 text-sm text-gray-custom line-clamp-2">{post.excerpt}</p>}
            </Link>
          </article>
        ))}
      </div>
    </section>
  )
}
