import { db } from "@/lib/db";
import { videos, categories } from "@/lib/db/schema";
import { eq } from "drizzle-orm";
import VideoCard from "@/components/VideoCard";
import { notFound } from "next/navigation";

export const dynamic = "force-dynamic";

export default async function CategoryPage({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  const category = (await db.select().from(categories).where(eq(categories.slug, slug)))[0];
  if (!category) return notFound();

  const vids = await db.select().from(videos).where(eq(videos.categoryId, category.id));

  return (
    <div className="max-w-7xl mx-auto">
      <h1 className="text-2xl font-bold mb-6">{category.name}</h1>
      <div className="grid grid-cols-2 md:grid-cols-4 gap-4">
        {vids.map((v) => <VideoCard key={v.id} video={v} />)}
        {vids.length === 0 && <p className="text-neutral-500 col-span-full">No videos in this category yet.</p>}
      </div>
    </div>
  );
}
