import { auth } from "@/lib/auth";
import prisma from "@/lib/prisma";
import { redirect } from "next/navigation";
import Link from "next/link";
import { OnboardingTour } from "@/components/shared/onboarding-tour";

export const metadata = { title: "Club Dashboard", robots: { index: false, follow: false } };

export default async function ClubDashboardPage() {
  const session = await auth();
  if (!session?.user) redirect("/login");

  const userId = parseInt(session.user.id);
  const userType = (session.user as Record<string, unknown>).userType as string;

  if (userType !== "strip_club") redirect("/dashboard");

  const [user, profileViews, linkedEscorts] = await Promise.all([
    prisma.user.findUnique({
      where: { id: userId },
      select: { username: true, active: true, hits: true },
    }),
    prisma.hit.count({ where: { user_id: userId } }).catch(() => 0),
    // Escorts linked to this venue (placeholder — uses favorites as a link mechanism)
    prisma.favorite
      .findMany({
        where: { user_id: userId, markable_type: "App\\Models\\User" },
      })
      .catch(() => []),
  ]);

  if (!user) redirect("/login");

  return (
    <div className="space-y-8">
      <OnboardingTour userTypeOverride="strip_club" />
      <h1 className="text-2xl font-bold text-text">Club / Venue Dashboard</h1>

      {/* Venue Stats */}
      <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
        {[
          { label: "Profile Views", value: user.hits },
          { label: "Linked Escorts", value: linkedEscorts.length },
          { label: "Events", value: 0 },
        ].map((stat) => (
          <div
            key={stat.label}
            className="bg-surface rounded-xl border border-surface-light p-6"
          >
            <p className="text-3xl font-bold text-text">{stat.value.toLocaleString()}</p>
            <p className="text-sm text-text-muted mt-1">{stat.label}</p>
          </div>
        ))}
      </div>

      {/* Quick Actions */}
      <div className="grid grid-cols-2 md:grid-cols-3 gap-4">
        {[
          { label: "Edit Venue", href: "/manage/profile/personal-information" },
          { label: "Manage Events", href: "/manage/events" },
          { label: "Link Escorts", href: "/manage/linked-escorts" },
        ].map((action) => (
          <Link
            key={action.label}
            href={action.href}
            className="bg-surface rounded-xl border border-surface-light p-4 text-center text-sm font-medium text-text hover:bg-surface-light transition-colors"
          >
            {action.label}
          </Link>
        ))}
      </div>

      {/* Event Calendar Placeholder */}
      <div className="bg-surface rounded-xl border border-surface-light p-6">
        <h2 className="text-lg font-semibold text-text mb-2">Event Calendar</h2>
        <p className="text-text-muted text-sm">
          The event calendar is coming soon. You will be able to create, manage, and promote
          events for your venue here.
        </p>
      </div>

      {/* Promo Tools Placeholder */}
      <div className="bg-surface rounded-xl border border-surface-light p-6">
        <h2 className="text-lg font-semibold text-text mb-2">Promotional Tools</h2>
        <p className="text-text-muted text-sm">
          Promotional tools are coming soon. Create special offers, featured listings, and
          targeted promotions to attract more visitors to your venue.
        </p>
      </div>
    </div>
  );
}
