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: "New Escorts — Just Joined | AdultWorld",
  description:
    "Meet the newest escorts on AdultWorld. Discover fresh faces who recently joined the platform, often offering introductory rates and eager to impress.",
  alternates: { canonical: "/escorts/new" },
  openGraph: {
    title: "New Escorts — Just Joined | AdultWorld",
    description:
      "Meet the newest escorts on AdultWorld. Discover fresh faces who recently joined the platform.",
  },
};

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

export default async function NewEscortsPage() {
  const thirtyDaysAgo = new Date();
  thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);

  const escortsRaw = await prisma.user.findMany({
    where: {
      user_type: "escort",
      active: true,
      banned_at: null,
      created_at: { gte: thirtyDaysAgo },
    },
    include: { country: true, city: true, _count: { select: { photos: true } } },
    orderBy: { created_at: "desc" },
    take: 48,
  });
  const escorts = await withAvatarUrls(escortsRaw);

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

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

        <div className="bg-surface rounded-lg p-6 space-y-3 text-text-muted leading-relaxed">
          <p>
            Fresh faces arrive on AdultWorld every day. This page showcases escorts who
            joined within the last 30 days, giving you early access to new companions
            before their schedules fill up. New escorts are often eager to build their
            reputation, which means attentive service and competitive introductory rates.
          </p>
          <p>
            Being among the first to book a new escort has its perks — you get personalised
            attention, more flexible scheduling, and the chance to leave the first review
            that helps shape their profile. Many of today&apos;s top-rated escorts started
            right here on this page.
          </p>
          <p>
            Every new listing goes through our standard verification checks, so quality and
            safety are never compromised. Scroll through the latest arrivals below and
            discover someone new.
          </p>
        </div>

        <p className="text-sm text-text-muted">
          Showing {escorts.length} of {totalCount.toLocaleString()} new escorts (last 30 days)
        </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 new escorts in the last 30 days</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/verified" className="text-primary hover:underline">
          Verified 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>
  );
}
