import { requireGuide } from "@/lib/auth-utils";
import { redirect } from "next/navigation";
import Link from "next/link";
import { Button } from "@/components/ui/button";
import { ArrowLeft } from "lucide-react";
import prisma from "@/lib/db";
import ArchivedTourCard from "./archived-tour-card";

export default async function ArchivedToursPage() {
  let user;
  try {
    user = await requireGuide();
  } catch {
    redirect("/login");
  }

  const archivedTours = await prisma.tour.findMany({
    where: {
      createdById: user.id,
      archived: true,
    },
    include: {
      images: {
        orderBy: {
          order: "asc",
        },
        take: 1,
      },
      _count: {
        select: {
          bookings: true,
          reviews: true,
        },
      },
    },
    orderBy: {
      updatedAt: "desc",
    },
  });

  return (
    <div className="container mx-auto px-4 py-8">
      <div className="mb-8">
        <Link href="/dashboard">
          <Button variant="ghost" size="sm" className="mb-4">
            <ArrowLeft className="mr-2 h-4 w-4" />
            Back to Dashboard
          </Button>
        </Link>
        <h1 className="text-3xl font-bold mb-2">Archived Tours</h1>
        <p className="text-muted-foreground">
          View and manage your archived tours
        </p>
      </div>

      {archivedTours.length === 0 ? (
        <div className="text-center py-12">
          <p className="text-muted-foreground">No archived tours</p>
        </div>
      ) : (
        <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
          {archivedTours.map((tour) => (
            <ArchivedTourCard key={tour.id} tour={tour} />
          ))}
        </div>
      )}
    </div>
  );
}
