import { redirect } from "next/navigation"
import { desc, eq } from "drizzle-orm"
import { db } from "@/lib/db"
import { activity_log } from "@/lib/db/schema"
import { getSessionUser } from "@/lib/session"
import { getEffectiveOwnerId } from "@/lib/account"
import { ActivityFeed } from "./activity-feed"

export const metadata = { title: "Activity" }

export default async function ActivityPage() {
  const user = await getSessionUser()
  if (!user) redirect("/login")

  const ownerId = await getEffectiveOwnerId(user.id)

  const activities = await db
    .select()
    .from(activity_log)
    .where(eq(activity_log.user_id, ownerId))
    .orderBy(desc(activity_log.created_at))
    .limit(100)

  return <ActivityFeed activities={activities ?? []} />
}
