import type { Metadata } from "next";
import { permanentRedirect } from "next/navigation";
import { getCurrentUser } from "@/lib/auth/dal";
import { listQuoteCategories, listQuotes, countQuotesByCategory } from "@/lib/quotes/queries";
import { QuotesBrowser } from "@/components/quotes/QuotesBrowser";
import { resolvePageMetadata } from "@/lib/seo/metadata";

export async function generateMetadata(): Promise<Metadata> {
  return resolvePageMetadata("/sozler", {
    title: "Güzel Sözler",
    description: "Kategorilere ayrılmış güzel sözleri oku, beğendiğini sevdiğin bir üyeye gönder.",
  });
}

export default async function QuotesPage({
  searchParams,
}: {
  searchParams: Promise<{ kategori?: string }>;
}) {
  // Eski ?kategori= bağlantıları (paylaşılmış linkler, arama motoru kayıtları) kalıcı olarak
  // yeni /sozler/[kategori] adresine yönlendirilir — SEO için tek, temiz bir URL yapısı.
  const { kategori } = await searchParams;
  if (kategori) {
    permanentRedirect(`/sozler/${kategori}`);
  }

  const viewer = await getCurrentUser();
  const [categories, quotes, countsByCategory] = await Promise.all([
    listQuoteCategories(),
    listQuotes(undefined, viewer?.id),
    countQuotesByCategory(),
  ]);

  return (
    <div className="relative overflow-hidden">
      <div className="pointer-events-none absolute -left-40 -top-20 h-[420px] w-[420px] rounded-full bg-[radial-gradient(circle,oklch(85%_0.07_335_/_.4),transparent_70%)]" />

      <div className="relative mx-auto max-w-6xl px-6 py-14">
        <div className="fade-up mb-10 text-center">
          <span className="mb-3 inline-block rounded-full bg-gradient-to-r from-accent to-accent-2 px-4 py-1.5 text-[12px] font-extrabold uppercase tracking-wide text-white">
            Güzel Sözler
          </span>
          <h1 className="text-[28px]">Gönlünce Seç, Sevdiğine Gönder</h1>
        </div>

        <QuotesBrowser
          categories={categories}
          countsByCategory={countsByCategory}
          activeCategorySlug={null}
          quotes={quotes}
          canSend={Boolean(viewer)}
          canLike={Boolean(viewer)}
        />
      </div>
    </div>
  );
}
