import prisma from "@/lib/prisma";
import Link from "next/link";
import type { Metadata } from "next";
import EscortCard from "@/components/shared/escort-card";
import { withAvatarUrls } from "@/lib/media";

export const metadata: Metadata = {
  title: "Trending Escorts | AdultWorld",
  description:
    "Most popular escorts this week on AdultWorld. See who is getting the most views, rising stars, and community favourites.",
  alternates: { canonical: "/escorts/trending" },
  openGraph: {
    title: "Trending Escorts | AdultWorld",
    description:
      "Most popular escorts this week on AdultWorld. See who is getting the most views.",
  },
};

export const revalidate = 600; // ISR (Stage 4): was force-dynamic

export default async function TrendingEscortsPage() {
  const escortsRaw = await prisma.user.findMany({
    where: { user_type: "escort", active: true, banned_at: null },
    include: { country: true, city: true, _count: { select: { photos: true } } },
    orderBy: { hits: "desc" },
    take: 48,
  });
  const escorts = await withAvatarUrls(escortsRaw);

  const totalCount = await prisma.user.count({
    where: { user_type: "escort", active: true, banned_at: null },
  });

  return (
    <div className="space-y-6">
      <div className="space-y-4">
        <h1 className="text-2xl md:text-3xl font-bold">Trending Escorts</h1>

        <div className="bg-surface rounded-lg p-6 space-y-3 text-text-muted leading-relaxed">
          <p>
            These are the most viewed escorts on AdultWorld right now. Trending profiles
            are ranked by page views, giving you a real-time look at who the community is
            most interested in. High view counts often reflect great photos, compelling
            descriptions, and excellent reviews.
          </p>
          <p>
            Rising stars appear here too — newer escorts whose profiles are gaining rapid
            attention. If you spot someone trending upward, it is a good sign they are
            making a strong impression. Community favourites tend to stay on this list
            thanks to repeat visitors and word-of-mouth recommendations.
          </p>
          <p>
            Keep in mind that popularity does not always mean availability. Trending escorts
            may have busier schedules, so booking in advance is recommended. Check their
            availability status and reach out early to secure your preferred time.
          </p>
        </div>

        <p className="text-sm text-text-muted">
          Showing top {escorts.length} of {totalCount.toLocaleString()} escorts by views
        </p>
      </div>

      <div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 gap-4">
        {escorts.map((escort) => (
          <EscortCard key={escort.id} escort={escort} />
        ))}
      </div>

      {escorts.length === 0 && (
        <div className="text-center py-16 text-text-muted">
          <p className="text-lg">No trending escorts found</p>
        </div>
      )}

      <div className="flex flex-wrap gap-3 text-sm">
        <Link href="/escorts" className="text-primary hover:underline">
          All Escorts
        </Link>
        <Link href="/escorts/new" className="text-primary hover:underline">
          New Escorts
        </Link>
        <Link href="/search" className="text-primary hover:underline">
          Advanced Search
        </Link>
        <Link href="/for-clients" className="text-primary hover:underline">
          Client Guide
        </Link>
      </div>
    </div>
  );
}
