import { getLocale, getTranslations } from 'next-intl/server'
import { Home, ShoppingBag, PackageSearch, MessageCircle, Newspaper, Tag, SearchX } from 'lucide-react'
import { Link } from '@/i18n/navigation'
import Container from '@/components/frontend/Container'
import { getHomeCategories } from '@/lib/db/queries/getHomeCategories'
import { getSiteInfo } from '@/lib/db/queries/getSiteInfo'

// Storefront 404 — rendered inside (root)/[locale]/layout.tsx, so it gets the
// real header/footer, translations and RTL. Shown for notFound() calls in
// storefront pages (unknown product/category/blog/page slug) and, via
// [...rest]/page.tsx, for any unmatched URL under a valid locale.
export default async function NotFound() {
  const [t, locale, siteInfo] = await Promise.all([
    getTranslations('NotFound'),
    getLocale(),
    getSiteInfo(),
  ])
  // Suggestions are best-effort — the 404 page must render even if the DB is down.
  const categories = await getHomeCategories(locale).catch(() => [])

  const quickLinks = [
    { href: '/track-order', label: t('trackOrder'), icon: PackageSearch },
    { href: '/offers', label: t('offers'), icon: Tag },
    { href: '/blogs', label: t('blog'), icon: Newspaper },
    { href: '/contact', label: t('contact'), icon: MessageCircle },
  ]

  return (
    <Container className="py-12 md:py-20">
      <title>{`${t('metaTitle')} | ${siteInfo.siteName}`}</title>

      <div className="max-w-2xl mx-auto text-center">
        {/* Illustration */}
        <div className="relative inline-flex items-center justify-center mb-6">
          <div className="absolute inset-0 bg-gradient-primary opacity-15 blur-3xl rounded-full" />
          <span
            aria-hidden
            className="relative text-[110px] md:text-[150px] font-black leading-none tracking-tight text-gradient-primary select-none"
          >
            404
          </span>
          <span className="absolute -bottom-2 end-0 md:-end-4 w-14 h-14 md:w-16 md:h-16 rounded-2xl bg-white shadow-lg border border-gray-100 flex items-center justify-center animate-bounce">
            <SearchX size={28} className="text-primary" />
          </span>
        </div>

        <div className="mb-4">
          <span className="inline-block bg-primary-soft text-primary text-xs font-semibold uppercase tracking-wide px-3 py-1 rounded-full">
            {t('badge')}
          </span>
        </div>
        <h1 className="text-2xl md:text-4xl font-bold text-dark mb-3">{t('title')}</h1>
        <p className="text-gray-custom md:text-lg mb-8">{t('description')}</p>

        <div className="flex flex-col sm:flex-row items-center justify-center gap-3">
          <Link
            href="/"
            className="w-full sm:w-auto inline-flex items-center justify-center gap-2 bg-gradient-primary text-white px-6 py-3 rounded-lg font-medium hover:shadow-md transition"
          >
            <Home size={18} />
            {t('goHome')}
          </Link>
          <Link
            href="/products"
            className="w-full sm:w-auto inline-flex items-center justify-center gap-2 border border-gray-200 text-dark px-6 py-3 rounded-lg font-medium hover:border-primary hover:text-primary transition"
          >
            <ShoppingBag size={18} />
            {t('browseProducts')}
          </Link>
        </div>
      </div>

      {/* Helpful links */}
      <div className="max-w-3xl mx-auto mt-14">
        <h2 className="text-sm font-semibold text-gray-custom uppercase tracking-wide text-center mb-4">
          {t('helpfulLinks')}
        </h2>
        <div className="grid grid-cols-2 md:grid-cols-4 gap-3">
          {quickLinks.map(({ href, label, icon: Icon }) => (
            <Link
              key={href}
              href={href}
              className="group bg-white rounded-xl border border-gray-100 shadow-sm p-4 flex flex-col items-center gap-2 hover:border-primary hover:shadow-md transition"
            >
              <span className="w-10 h-10 rounded-full bg-primary-soft text-primary flex items-center justify-center group-hover:bg-primary group-hover:text-white transition">
                <Icon size={18} />
              </span>
              <span className="text-sm font-medium text-dark">{label}</span>
            </Link>
          ))}
        </div>
      </div>

      {/* Popular categories */}
      {categories.length > 0 && (
        <div className="max-w-3xl mx-auto mt-10">
          <h2 className="text-sm font-semibold text-gray-custom uppercase tracking-wide text-center mb-4">
            {t('popularCategories')}
          </h2>
          <div className="flex flex-wrap justify-center gap-2">
            {categories.slice(0, 8).map((category) => (
              <Link
                key={category.id}
                href={`/category/${category.slug}`}
                className="inline-flex items-center gap-2 bg-white border border-gray-200 rounded-full ps-1.5 pe-4 py-1.5 text-sm text-dark hover:border-primary hover:text-primary transition"
              >
                {/* Background image, same as CategoryCarouselClient — category
                    icons aren't all next/image-compatible (host/format). */}
                <span
                  aria-hidden
                  className="w-7 h-7 rounded-full bg-light-gray bg-cover bg-center shrink-0"
                  style={category.imageUrl ? { backgroundImage: `url(${category.imageUrl})` } : undefined}
                />
                {category.name}
              </Link>
            ))}
          </div>
        </div>
      )}
    </Container>
  )
}
